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_zoneStart = infoXml.attribute("zonein", "0").toInt();
113 m_zoneEnd = infoXml.attribute("zoneout", "100").toInt();
114 setProfilePath(profilePath);
118 QDomNode tracksinfo = m_document.elementsByTagName("tracksinfo").at(0);
119 TrackInfo projectTrack;
120 if (!tracksinfo.isNull()) {
121 QDomNodeList trackslist = tracksinfo.childNodes();
122 int maxchild = trackslist.count();
123 for (int k = 0; k < maxchild; k++) {
124 e = trackslist.at(k).toElement();
125 if (e.tagName() == "trackinfo") {
126 if (e.attribute("type") == "audio") projectTrack.type = AUDIOTRACK;
127 else projectTrack.type = VIDEOTRACK;
128 projectTrack.isMute = e.attribute("mute").toInt();
129 projectTrack.isBlind = e.attribute("blind").toInt();
130 projectTrack.isLocked = e.attribute("locked").toInt();
131 m_tracksList.append(projectTrack);
134 westley.removeChild(tracksinfo);
137 QDomNodeList producers = m_document.elementsByTagName("producer");
138 QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
139 const int max = producers.count();
140 const int infomax = infoproducers.count();
142 QDomNodeList folders = m_document.elementsByTagName("folder");
143 for (int i = 0; i < folders.count(); i++) {
144 e = folders.item(i).cloneNode().toElement();
145 m_clipManager->addFolder(e.attribute("id"), e.attribute("name"));
149 m_documentLoadingStep = 100.0 / (max + infomax + m_document.elementsByTagName("entry").count());
150 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
154 for (int i = 0; i < infomax && !m_abortLoading; i++) {
155 e = infoproducers.item(i).cloneNode().toElement();
156 if (m_documentLoadingStep > 0) {
157 m_documentLoadingProgress += m_documentLoadingStep;
158 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
159 //qApp->processEvents();
161 QString prodId = e.attribute("id");
162 if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion") && !m_abortLoading) {
163 e.setTagName("producer");
164 // Get MLT's original producer properties
166 for (int j = 0; j < max; j++) {
167 QDomElement o = producers.item(j).cloneNode().toElement();
168 QString origId = o.attribute("id").section('_', 0, 0);
169 if (origId == prodId) {
174 addClipInfo(e, orig, prodId);
175 kDebug() << "// NLIVE PROD: " << prodId;
178 if (m_abortLoading) {
179 //parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
180 emit resetProjectList();
183 m_tracksList.clear();
184 kWarning() << "Aborted loading of: " << url.path();
185 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
186 setProfilePath(KdenliveSettings::default_profile());
187 m_clipManager->clear();
189 QDomNode markers = m_document.elementsByTagName("markers").at(0);
190 if (!markers.isNull()) {
191 QDomNodeList markerslist = markers.childNodes();
192 int maxchild = markerslist.count();
193 for (int k = 0; k < maxchild; k++) {
194 e = markerslist.at(k).toElement();
195 if (e.tagName() == "marker") {
196 m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
199 westley.removeChild(markers);
201 m_document.removeChild(infoXmlNode);
202 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
206 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
207 kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
208 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
210 setProfilePath(KdenliveSettings::default_profile());
212 KIO::NetAccess::removeTempFile(tmpFile);
214 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
216 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
217 setProfilePath(KdenliveSettings::default_profile());
220 m_document = createEmptyDocument(tracks.x(), tracks.y());
221 setProfilePath(profileName);
223 if (m_projectFolder.isEmpty()) m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
225 // make sure that the necessary folders exist
226 KStandardDirs::makeDir(m_projectFolder.path() + "/titles/");
227 KStandardDirs::makeDir(m_projectFolder.path() + "/thumbs/");
228 KStandardDirs::makeDir(m_projectFolder.path() + "/ladspa/");
230 kDebug() << "KDEnlive document, init timecode: " << m_fps;
231 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
232 else m_timecode.setFormat((int) m_fps);
234 //kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
235 connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
239 KdenliveDoc::~KdenliveDoc()
241 delete m_commandStack;
242 delete m_clipManager;
243 delete m_autoSaveTimer;
245 m_autosave->remove();
250 void KdenliveDoc::setSceneList()
252 m_render->setSceneList(m_document.toString(), m_startPos);
253 // m_document xml is now useless, clear it
258 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks)
260 // Creating new document
262 QDomElement westley = doc.createElement("westley");
263 doc.appendChild(westley);
266 TrackInfo videoTrack;
267 videoTrack.type = VIDEOTRACK;
268 videoTrack.isMute = false;
269 videoTrack.isBlind = false;
270 videoTrack.isLocked = false;
272 TrackInfo audioTrack;
273 audioTrack.type = AUDIOTRACK;
274 audioTrack.isMute = false;
275 audioTrack.isBlind = true;
276 audioTrack.isLocked = false;
278 QDomElement tractor = doc.createElement("tractor");
279 tractor.setAttribute("id", "maintractor");
280 QDomElement multitrack = doc.createElement("multitrack");
281 QDomElement playlist = doc.createElement("playlist");
282 playlist.setAttribute("id", "black_track");
283 westley.appendChild(playlist);
287 int total = audiotracks + videotracks + 1;
289 for (int i = 1; i < total; i++) {
290 QDomElement playlist = doc.createElement("playlist");
291 playlist.setAttribute("id", "playlist" + QString::number(i));
292 westley.appendChild(playlist);
295 QDomElement track0 = doc.createElement("track");
296 track0.setAttribute("producer", "black_track");
297 tractor.appendChild(track0);
299 // create audio tracks
300 for (int i = 1; i < audiotracks + 1; i++) {
301 QDomElement track = doc.createElement("track");
302 track.setAttribute("producer", "playlist" + QString::number(i));
303 track.setAttribute("hide", "video");
304 tractor.appendChild(track);
305 m_tracksList.append(audioTrack);
308 // create video tracks
309 for (int i = audiotracks + 1; i < total; i++) {
310 QDomElement track = doc.createElement("track");
311 track.setAttribute("producer", "playlist" + QString::number(i));
312 tractor.appendChild(track);
313 m_tracksList.append(videoTrack);
316 for (int i = 2; i < total ; i++) {
317 QDomElement transition = doc.createElement("transition");
318 transition.setAttribute("always_active", "1");
320 QDomElement property = doc.createElement("property");
321 property.setAttribute("name", "a_track");
322 QDomText value = doc.createTextNode(QString::number(1));
323 property.appendChild(value);
324 transition.appendChild(property);
326 property = doc.createElement("property");
327 property.setAttribute("name", "b_track");
328 value = doc.createTextNode(QString::number(i));
329 property.appendChild(value);
330 transition.appendChild(property);
332 property = doc.createElement("property");
333 property.setAttribute("name", "mlt_service");
334 value = doc.createTextNode("mix");
335 property.appendChild(value);
336 transition.appendChild(property);
338 property = doc.createElement("property");
339 property.setAttribute("name", "combine");
340 value = doc.createTextNode("1");
341 property.appendChild(value);
342 transition.appendChild(property);
344 property = doc.createElement("property");
345 property.setAttribute("name", "internal_added");
346 value = doc.createTextNode("237");
347 property.appendChild(value);
348 transition.appendChild(property);
349 tractor.appendChild(transition);
351 westley.appendChild(tractor);
356 void KdenliveDoc::syncGuides(QList <Guide *> guides)
359 QDomElement guideNode = m_guidesXml.createElement("guides");
360 m_guidesXml.appendChild(guideNode);
363 for (int i = 0; i < guides.count(); i++) {
364 e = m_guidesXml.createElement("guide");
365 e.setAttribute("time", guides.at(i)->position().ms() / 1000);
366 e.setAttribute("comment", guides.at(i)->label());
367 guideNode.appendChild(e);
369 emit guidesUpdated();
372 QDomElement KdenliveDoc::guidesXml() const
374 return m_guidesXml.documentElement();
377 void KdenliveDoc::slotAutoSave()
379 if (m_render && m_autosave) {
380 if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
381 // show error: could not open the autosave file
382 kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
384 kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
386 if (KdenliveSettings::dropbframes()) {
387 KdenliveSettings::setDropbframes(false);
388 m_clipManager->updatePreviewSettings();
389 doc = m_render->sceneList();
390 KdenliveSettings::setDropbframes(true);
391 m_clipManager->updatePreviewSettings();
392 } else doc = m_render->sceneList();
393 saveSceneList(m_autosave->fileName(), doc);
397 void KdenliveDoc::setZoom(int factor)
402 int KdenliveDoc::zoom() const
407 bool KdenliveDoc::convertDocument(double version)
409 kDebug() << "Opening a document with version " << version;
410 const double current_version = 0.82;
412 if (version == current_version) return true;
414 if (version > current_version) {
415 kDebug() << "Unable to open document with version " << version;
416 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"));
420 // Opening a old Kdenlive document
421 if (version == 0.5 || version == 0.7) {
422 KMessageBox::sorry(kapp->activeWindow(), i18n("This project type is unsupported (version %1) and can't be loaded.", version), i18n("Unable to open project"));
423 kDebug() << "Unable to open document with version " << version;
424 // TODO: convert 0.7 (0.5?) files to the new document format.
430 if (version == 0.81) {
431 // Add correct tracks info
432 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
433 QDomElement infoXml = kdenlivedoc.toElement();
434 infoXml.setAttribute("version", current_version);
435 QString currentTrackOrder = infoXml.attribute("tracks");
436 QDomElement tracksinfo = m_document.createElement("tracksinfo");
437 for (int i = 0; i < currentTrackOrder.size(); i++) {
438 QDomElement trackinfo = m_document.createElement("trackinfo");
439 if (currentTrackOrder.data()[i] == 'a') {
440 trackinfo.setAttribute("type", "audio");
441 trackinfo.setAttribute("blind", true);
442 } else trackinfo.setAttribute("blind", false);
443 trackinfo.setAttribute("mute", false);
444 trackinfo.setAttribute("locked", false);
445 tracksinfo.appendChild(trackinfo);
447 infoXml.appendChild(tracksinfo);
451 if (version == 0.8) {
452 // Add the tracks information
453 QDomNodeList tracks = m_document.elementsByTagName("track");
454 int max = tracks.count();
456 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
457 QDomElement infoXml = kdenlivedoc.toElement();
458 infoXml.setAttribute("version", current_version);
459 QDomElement tracksinfo = m_document.createElement("tracksinfo");
461 for (int i = 0; i < max; i++) {
462 QDomElement trackinfo = m_document.createElement("trackinfo");
463 QDomElement t = tracks.at(i).toElement();
464 if (t.attribute("hide") == "video") {
465 trackinfo.setAttribute("type", "audio");
466 trackinfo.setAttribute("blind", true);
467 } else trackinfo.setAttribute("blind", false);
468 trackinfo.setAttribute("mute", false);
469 trackinfo.setAttribute("locked", false);
470 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
472 infoXml.appendChild(tracksinfo);
476 QDomNode westley = m_document.elementsByTagName("westley").at(1);
477 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
478 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
479 QDomElement kdenlivedoc_old = kdenlivedoc.cloneNode(true).toElement(); // Needed for folders
480 QDomElement infoXml = kdenlivedoc.toElement();
481 infoXml.setAttribute("version", current_version);
482 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
483 QDomNodeList playlists = m_document.elementsByTagName("playlist");
485 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
486 QString profile = props.toElement().attribute("videoprofile");
487 m_startPos = props.toElement().attribute("timeline_position").toInt();
488 if (profile == "dv_wide") profile = "dv_pal_wide";
490 // move playlists outside of tractor and add the tracks instead
491 int max = playlists.count();
492 if (westley.isNull()) {
493 westley = m_document.createElement("westley");
494 m_document.documentElement().appendChild(westley);
496 if (tractor.isNull()) {
497 kDebug() << "// NO WESTLEY PLAYLIST, building empty one";
498 QDomElement blank_tractor = m_document.createElement("tractor");
499 westley.appendChild(blank_tractor);
500 QDomElement blank_playlist = m_document.createElement("playlist");
501 blank_playlist.setAttribute("id", "black_track");
502 westley.insertBefore(blank_playlist, QDomNode());
503 QDomElement blank_track = m_document.createElement("track");
504 blank_track.setAttribute("producer", "black_track");
505 blank_tractor.appendChild(blank_track);
507 QDomNodeList kdenlivetracks = m_document.elementsByTagName("kdenlivetrack");
508 for (int i = 0; i < kdenlivetracks.count(); i++) {
509 blank_playlist = m_document.createElement("playlist");
510 blank_playlist.setAttribute("id", "playlist" + QString::number(i));
511 westley.insertBefore(blank_playlist, QDomNode());
512 blank_track = m_document.createElement("track");
513 blank_track.setAttribute("producer", "playlist" + QString::number(i));
514 blank_tractor.appendChild(blank_track);
515 if (kdenlivetracks.at(i).toElement().attribute("cliptype") == "Sound") {
516 blank_playlist.setAttribute("hide", "video");
517 blank_track.setAttribute("hide", "video");
520 } else for (int i = 0; i < max; i++) {
521 QDomNode n = playlists.at(i);
522 westley.insertBefore(n, QDomNode());
523 QDomElement pl = n.toElement();
524 QDomElement track = m_document.createElement("track");
525 QString trackType = pl.attribute("hide");
526 if (!trackType.isEmpty())
527 track.setAttribute("hide", trackType);
528 QString playlist_id = pl.attribute("id");
529 if (playlist_id.isEmpty()) {
530 playlist_id = "black_track";
531 pl.setAttribute("id", playlist_id);
533 track.setAttribute("producer", playlist_id);
534 //tractor.appendChild(track);
535 #define KEEP_TRACK_ORDER 1
536 #ifdef KEEP_TRACK_ORDER
537 tractor.insertAfter(track, QDomNode());
539 // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
540 // insertion sort - O( tracks*tracks )
541 // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
542 QDomElement tractor_elem = tractor.toElement();
543 if (! tractor_elem.isNull()) {
544 QDomNodeList tracks = tractor_elem.elementsByTagName("track");
545 int size = tracks.size();
547 tractor.insertAfter(track, QDomNode());
549 bool inserted = false;
550 for (int i = 0; i < size; ++i) {
551 QDomElement track_elem = tracks.at(i).toElement();
552 if (track_elem.isNull()) {
553 tractor.insertAfter(track, QDomNode());
557 kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
558 if (playlist_id < track_elem.attribute("producer")) {
559 tractor.insertBefore(track, track_elem);
565 // Reach here, no insertion, insert last
567 tractor.insertAfter(track, QDomNode());
571 kWarning() << "tractor was not a QDomElement";
572 tractor.insertAfter(track, QDomNode());
576 tractor.removeChild(multitrack);
578 //Â audio track mixing transitions should not be added to track view, so add required attribute
579 QDomNodeList transitions = m_document.elementsByTagName("transition");
580 max = transitions.count();
581 for (int i = 0; i < max; i++) {
582 QDomElement tr = transitions.at(i).toElement();
583 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
584 QDomElement property = m_document.createElement("property");
585 property.setAttribute("name", "internal_added");
586 QDomText value = m_document.createTextNode("237");
587 property.appendChild(value);
588 tr.appendChild(property);
589 property = m_document.createElement("property");
590 property.setAttribute("name", "mlt_service");
591 value = m_document.createTextNode("mix");
592 property.appendChild(value);
593 tr.appendChild(property);
595 // convert transition
596 QDomNamedNodeMap attrs = tr.attributes();
597 for (int j = 0; j < attrs.count(); j++) {
598 QString attrName = attrs.item(j).nodeName();
599 if (attrName != "in" && attrName != "out" && attrName != "id") {
600 QDomElement property = m_document.createElement("property");
601 property.setAttribute("name", attrName);
602 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
603 property.appendChild(value);
604 tr.appendChild(property);
610 //Â move transitions after tracks
611 for (int i = 0; i < max; i++) {
612 tractor.insertAfter(transitions.at(0), QDomNode());
615 // Fix filters format
616 QDomNodeList entries = m_document.elementsByTagName("entry");
617 max = entries.count();
618 for (int i = 0; i < max; i++) {
621 QDomNode m = entries.at(i).firstChild();
622 while (!m.isNull()) {
623 if (m.toElement().tagName() == "filter") {
624 QDomElement filt = m.toElement();
625 QDomNamedNodeMap attrs = filt.attributes();
626 QString current_id = filt.attribute("kdenlive_id");
627 if (current_id != last_id) {
629 last_id = current_id;
631 QDomElement e = m_document.createElement("property");
632 e.setAttribute("name", "kdenlive_ix");
633 QDomText value = m_document.createTextNode(QString::number(effectix));
634 e.appendChild(value);
636 for (int j = 0; j < attrs.count(); j++) {
637 QDomAttr a = attrs.item(j).toAttr();
639 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
640 QDomElement e = m_document.createElement("property");
641 e.setAttribute("name", a.name());
642 QDomText value = m_document.createTextNode(a.value());
643 e.appendChild(value);
654 QDomNodeList filters = m_document.elementsByTagName("filter");
655 max = filters.count();
658 for (int i = 0; i < max; i++) {
659 QDomElement filt = filters.at(i).toElement();
660 QDomNamedNodeMap attrs = filt.attributes();
661 QString current_id = filt.attribute("kdenlive_id");
662 if (current_id != last_id) {
664 last_id = current_id;
666 QDomElement e = m_document.createElement("property");
667 e.setAttribute("name", "kdenlive_ix");
668 QDomText value = m_document.createTextNode(QString::number(1));
669 e.appendChild(value);
671 for (int j = 0; j < attrs.count(); j++) {
672 QDomAttr a = attrs.item(j).toAttr();
674 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
675 QDomElement e = m_document.createElement("property");
676 e.setAttribute("name", a.name());
677 QDomText value = m_document.createTextNode(a.value());
678 e.appendChild(value);
685 QDomNodeList producers = westley.toElement().elementsByTagName("producer");
686 max = producers.count();
687 for (int i = 0; i < max; i++) {
688 QDomElement prod = producers.at(i).toElement();
689 if (prod.attribute("mlt_service") == "framebuffer") {
690 QString slowmotionprod = prod.attribute("resource");
691 slowmotionprod.replace(':', '?');
692 kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
693 prod.setAttribute("resource", slowmotionprod);
696 //Â move producers to correct place, markers to a global list, fix clip descriptions
697 QDomElement markers = m_document.createElement("markers");
698 // This will get the westley producers:
699 producers = m_document.elementsByTagName("producer");
700 max = producers.count();
701 for (int i = 0; i < max; i++) {
702 QDomElement prod = producers.at(0).toElement();
703 // add resource also as a property (to allow path correction in setNewResource())
704 // TODO: will it work with slowmotion? needs testing
705 /*if (!prod.attribute("resource").isEmpty()) {
706 QDomElement prop_resource = m_document.createElement("property");
707 prop_resource.setAttribute("name", "resource");
708 QDomText resource = m_document.createTextNode(prod.attribute("resource"));
709 prop_resource.appendChild(resource);
710 prod.appendChild(prop_resource);
712 QDomNode m = prod.firstChild();
714 if (m.toElement().tagName() == "markers") {
715 QDomNodeList prodchilds = m.childNodes();
716 int maxchild = prodchilds.count();
717 for (int k = 0; k < maxchild; k++) {
718 QDomElement mark = prodchilds.at(0).toElement();
719 mark.setAttribute("id", prod.attribute("id"));
720 markers.insertAfter(mark, QDomNode());
723 } else if (prod.attribute("type").toInt() == TEXT) {
724 // convert title clip
725 if (m.toElement().tagName() == "textclip") {
727 QDomElement titleclip = m.toElement();
728 QDomElement title = tdoc.createElement("kdenlivetitle");
729 tdoc.appendChild(title);
730 QDomNodeList objects = titleclip.childNodes();
731 int maxchild = objects.count();
732 for (int k = 0; k < maxchild; k++) {
734 QDomElement ob = objects.at(k).toElement();
735 if (ob.attribute("type") == "3") {
736 // text object - all of this goes into "xmldata"...
737 QDomElement item = tdoc.createElement("item");
738 item.setAttribute("z-index", ob.attribute("z"));
739 item.setAttribute("type", "QGraphicsTextItem");
740 QDomElement position = tdoc.createElement("position");
741 position.setAttribute("x", ob.attribute("x"));
742 position.setAttribute("y", ob.attribute("y"));
743 QDomElement content = tdoc.createElement("content");
744 content.setAttribute("font", ob.attribute("font_family"));
745 content.setAttribute("font-size", ob.attribute("font_size"));
746 content.setAttribute("font-bold", ob.attribute("bold"));
747 content.setAttribute("font-italic", ob.attribute("italic"));
748 content.setAttribute("font-underline", ob.attribute("underline"));
749 QString col = ob.attribute("color");
751 content.setAttribute("font-color", colorToString(c));
752 // todo: These fields are missing from the newly generated xmldata:
753 // transform, startviewport, endviewport, background
755 QDomText conttxt = tdoc.createTextNode(ob.attribute("text"));
756 content.appendChild(conttxt);
757 item.appendChild(position);
758 item.appendChild(content);
759 title.appendChild(item);
760 } else if (ob.attribute("type") == "5") {
762 QDomElement item = tdoc.createElement("item");
763 item.setAttribute("z-index", ob.attribute("z"));
764 item.setAttribute("type", "QGraphicsRectItem");
765 QDomElement position = tdoc.createElement("position");
766 position.setAttribute("x", ob.attribute("x"));
767 position.setAttribute("y", ob.attribute("y"));
768 QDomElement content = tdoc.createElement("content");
769 QString col = ob.attribute("color");
771 content.setAttribute("brushcolor", colorToString(c));
772 QString rect = "0,0,";
773 rect.append(ob.attribute("width"));
775 rect.append(ob.attribute("height"));
776 content.setAttribute("rect", rect);
777 item.appendChild(position);
778 item.appendChild(content);
779 title.appendChild(item);
782 prod.setAttribute("xmldata", tdoc.toString());
783 // mbd todo: This clearly does not work, as every title gets the same name - trying to leave it empty
784 // QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
785 // prod.setAttribute("titlename", titleInfo.at(0));
786 // prod.setAttribute("resource", titleInfo.at(1));
787 //kDebug()<<"TITLE DATA:\n"<<tdoc.toString();
789 } // End conversion of title clips.
791 } else if (m.isText()) {
792 QString comment = m.nodeValue();
793 if (!comment.isEmpty()) {
794 prod.setAttribute("description", comment);
799 int duration = prod.attribute("duration").toInt();
800 if (duration > 0) prod.setAttribute("out", QString::number(duration));
801 // The clip goes back in, but text clips should not go back in, at least not modified
802 westley.insertBefore(prod, QDomNode());
806 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
807 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
810 // Convert as much of the kdenlivedoc as possible. Use the producer in westley
811 // First, remove the old stuff from westley, and add a new empty one
812 // Also, track the max id in order to use it for the adding of groups/folders
813 int max_kproducer_id = 0;
814 westley0.removeChild(kdenlivedoc);
815 QDomElement kdenlivedoc_new = m_document.createElement("kdenlivedoc");
816 kdenlivedoc_new.setAttribute("profile", profile);
819 QDomNodeList tracks = m_document.elementsByTagName("track");
820 max = tracks.count();
821 QDomElement tracksinfo = m_document.createElement("tracksinfo");
822 for (int i = 0; i < max; i++) {
823 QDomElement trackinfo = m_document.createElement("trackinfo");
824 QDomElement t = tracks.at(i).toElement();
825 if (t.attribute("hide") == "video") {
826 trackinfo.setAttribute("type", "audio");
827 trackinfo.setAttribute("blind", true);
828 } else trackinfo.setAttribute("blind", false);
829 trackinfo.setAttribute("mute", false);
830 trackinfo.setAttribute("locked", false);
831 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
833 kdenlivedoc_new.appendChild(tracksinfo);
835 // Add all the producers that has a ressource in westley
836 QDomElement westley_element = westley0.toElement();
837 if (westley_element.isNull()) {
838 kWarning() << "westley0 element in document was not a QDomElement - unable to add producers to new kdenlivedoc";
840 QDomNodeList wproducers = westley_element.elementsByTagName("producer");
841 int kmax = wproducers.count();
842 for (int i = 0; i < kmax; i++) {
843 QDomElement wproducer = wproducers.at(i).toElement();
844 if (wproducer.isNull()) {
845 kWarning() << "Found producer in westley0, that was not a QDomElement";
848 if (wproducer.attribute("id") == "black") continue;
849 // We have to do slightly different things, depending on the type
850 kDebug() << "Converting producer element with type" << wproducer.attribute("type");
851 if (wproducer.attribute("type").toInt() == TEXT) {
852 kDebug() << "Found TEXT element in producer" << endl;
853 QDomElement kproducer = wproducer.cloneNode(true).toElement();
854 kproducer.setTagName("kdenlive_producer");
855 kdenlivedoc_new.appendChild(kproducer);
856 // TODO: Perhaps needs some more changes here to "frequency", aspect ratio as a float, frame_size, channels, and later, ressource and title name
858 QDomElement kproducer = m_document.createElement("kdenlive_producer");
859 kproducer.setAttribute("id", wproducer.attribute("id"));
860 if (!wproducer.attribute("description").isEmpty())
861 kproducer.setAttribute("description", wproducer.attribute("description"));
862 kproducer.setAttribute("resource", wproducer.attribute("resource"));
863 kproducer.setAttribute("type", wproducer.attribute("type"));
864 // Testing fix for 358
865 if (!wproducer.attribute("aspect_ratio").isEmpty()) {
866 kproducer.setAttribute("aspect_ratio", wproducer.attribute("aspect_ratio"));
868 if (!wproducer.attribute("source_fps").isEmpty()) {
869 kproducer.setAttribute("fps", wproducer.attribute("source_fps"));
871 if (!wproducer.attribute("length").isEmpty()) {
872 kproducer.setAttribute("duration", wproducer.attribute("length"));
874 kdenlivedoc_new.appendChild(kproducer);
876 if (wproducer.attribute("id").toInt() > max_kproducer_id) {
877 max_kproducer_id = wproducer.attribute("id").toInt();
881 #define LOOKUP_FOLDER 1
883 // Look through all the folder elements of the old doc, for each folder, for each producer,
884 // get the id, look it up in the new doc, set the groupname and groupid
885 // Note, this does not work at the moment - at least one folders shows up missing, and clips with no folder
887 // QDomElement kdenlivedoc_old = kdenlivedoc.toElement();
888 if (!kdenlivedoc_old.isNull()) {
889 QDomNodeList folders = kdenlivedoc_old.elementsByTagName("folder");
890 int fsize = folders.size();
891 int groupId = max_kproducer_id + 1; // Start at +1 of max id of the kdenlive_producers
892 for (int i = 0; i < fsize; ++i) {
893 QDomElement folder = folders.at(i).toElement();
894 if (!folder.isNull()) {
895 QString groupName = folder.attribute("name");
896 kDebug() << "groupName: " << groupName << " with groupId: " << groupId;
897 QDomNodeList fproducers = folder.elementsByTagName("producer");
898 int psize = fproducers.size();
899 for (int j = 0; j < psize; ++j) {
900 QDomElement fproducer = fproducers.at(j).toElement();
901 if (!fproducer.isNull()) {
902 QString id = fproducer.attribute("id");
903 // This is not very effective, but compared to loading the clips, its a breeze
904 QDomNodeList kdenlive_producers = kdenlivedoc_new.elementsByTagName("kdenlive_producer");
905 int kpsize = kdenlive_producers.size();
906 for (int k = 0; k < kpsize; ++k) {
907 QDomElement kproducer = kdenlive_producers.at(k).toElement(); // Its an element for sure
908 if (id == kproducer.attribute("id")) {
909 // We do not check that it already is part of a folder
910 kproducer.setAttribute("groupid", groupId);
911 kproducer.setAttribute("groupname", groupName);
922 westley0.appendChild(kdenlivedoc_new);
924 QDomNodeList elements = westley.childNodes();
925 max = elements.count();
926 for (int i = 0; i < max; i++) {
927 QDomElement prod = elements.at(0).toElement();
928 westley0.insertAfter(prod, QDomNode());
931 westley0.removeChild(westley);
933 // experimental and probably slow
934 // adds <avfile /> information to <kdenlive_producer />
935 QDomNodeList kproducers = m_document.elementsByTagName("kdenlive_producer");
936 QDomNodeList avfiles = kdenlivedoc_old.elementsByTagName("avfile");
937 kDebug() << "found" << avfiles.count() << "<avfile />s and" << kproducers.count() << "<kdenlive_producer />s";
938 for (int i = 0; i < avfiles.count(); ++i) {
939 QDomElement avfile = avfiles.at(i).toElement();
940 QDomElement kproducer;
942 kWarning() << "found an <avfile /> that is not a QDomElement";
944 QString id = avfile.attribute("id");
945 // this is horrible, must be rewritten, it's just for test
946 for (int j = 0; j < kproducers.count(); ++j) {
947 //kDebug() << "checking <kdenlive_producer /> with id" << kproducers.at(j).toElement().attribute("id");
948 if (kproducers.at(j).toElement().attribute("id") == id) {
949 kproducer = kproducers.at(j).toElement();
953 if (kproducer == QDomElement())
954 kWarning() << "no match for <avfile /> with id =" << id;
956 //kDebug() << "ready to set additional <avfile />'s attributes (id =" << id << ")";
957 kproducer.setAttribute("channels", avfile.attribute("channels"));
958 kproducer.setAttribute("duration", avfile.attribute("duration"));
959 kproducer.setAttribute("frame_size", avfile.attribute("width") + 'x' + avfile.attribute("height"));
960 kproducer.setAttribute("frequency", avfile.attribute("frequency"));
961 if (kproducer.attribute("description").isEmpty() && !avfile.attribute("description").isEmpty())
962 kproducer.setAttribute("description", avfile.attribute("description"));
967 /*kDebug() << "///////////////// CONVERTEDÂ DOC:";
968 kDebug() << m_document.toString();
969 kDebug() << "///////////////// END CONVERTEDÂ DOC:";
971 QFile file("converted.kdenlive");
972 if (file.open(QIODevice::WriteOnly)) {
973 QTextStream stream(&file);
974 stream << m_document.toString().toUtf8();
977 kDebug() << "Unable to dump file to converted.kdenlive";
980 //kDebug() << "///////////////// END CONVERTEDÂ DOC:";
985 QString KdenliveDoc::colorToString(const QColor& c)
987 QString ret = "%1,%2,%3,%4";
988 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
992 void KdenliveDoc::setZone(int start, int end)
998 QPoint KdenliveDoc::zone() const
1000 return QPoint(m_zoneStart, m_zoneEnd);
1003 bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
1005 QDomDocument sceneList;
1006 sceneList.setContent(scene, true);
1007 QDomNode wes = sceneList.elementsByTagName("westley").at(0);
1008 QDomElement addedXml = sceneList.createElement("kdenlivedoc");
1009 wes.appendChild(addedXml);
1011 QDomElement markers = sceneList.createElement("markers");
1012 addedXml.setAttribute("version", "0.82");
1013 addedXml.setAttribute("profile", profilePath());
1014 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
1015 addedXml.setAttribute("zonein", m_zoneStart);
1016 addedXml.setAttribute("zoneout", m_zoneEnd);
1017 addedXml.setAttribute("projectfolder", m_projectFolder.path());
1018 addedXml.setAttribute("zoom", m_zoom);
1021 QDomElement tracksinfo = sceneList.createElement("tracksinfo");
1022 foreach(const TrackInfo &info, m_tracksList) {
1023 QDomElement trackinfo = sceneList.createElement("trackinfo");
1024 if (info.type == AUDIOTRACK) trackinfo.setAttribute("type", "audio");
1025 trackinfo.setAttribute("mute", info.isMute);
1026 trackinfo.setAttribute("blind", info.isBlind);
1027 trackinfo.setAttribute("locked", info.isLocked);
1028 tracksinfo.appendChild(trackinfo);
1030 addedXml.appendChild(tracksinfo);
1032 // save project folders
1033 QMap <QString, QString> folderlist = m_clipManager->documentFolderList();
1035 QMapIterator<QString, QString> f(folderlist);
1036 while (f.hasNext()) {
1038 QDomElement folder = sceneList.createElement("folder");
1039 folder.setAttribute("id", f.key());
1040 folder.setAttribute("name", f.value());
1041 addedXml.appendChild(folder);
1044 // Save project clips
1046 QList <DocClipBase*> list = m_clipManager->documentClipList();
1047 for (int i = 0; i < list.count(); i++) {
1048 e = list.at(i)->toXML();
1049 e.setTagName("kdenlive_producer");
1050 addedXml.appendChild(sceneList.importNode(e, true));
1051 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
1052 for (int j = 0; j < marks.count(); j++) {
1053 QDomElement marker = sceneList.createElement("marker");
1054 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
1055 marker.setAttribute("comment", marks.at(j).comment());
1056 marker.setAttribute("id", e.attribute("id"));
1057 markers.appendChild(marker);
1060 addedXml.appendChild(markers);
1063 if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml.documentElement(), true));
1066 addedXml.appendChild(sceneList.importNode(m_clipManager->groupsXml(), true));
1068 //wes.appendChild(doc.importNode(kdenliveData, true));
1071 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1072 kWarning() << "////// ERROR writing to file: " << path;
1073 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1077 file.write(sceneList.toString().toUtf8());
1078 if (file.error() != QFile::NoError) {
1079 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1087 ClipManager *KdenliveDoc::clipManager()
1089 return m_clipManager;
1092 KUrl KdenliveDoc::projectFolder() const
1094 //if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
1095 return m_projectFolder;
1098 void KdenliveDoc::setProjectFolder(KUrl url)
1100 if (url == m_projectFolder) return;
1102 KStandardDirs::makeDir(url.path());
1103 KStandardDirs::makeDir(url.path() + "/titles/");
1104 KStandardDirs::makeDir(url.path() + "/thumbs/");
1105 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);
1106 m_projectFolder = url;
1109 void KdenliveDoc::moveProjectData(KUrl url)
1111 QList <DocClipBase*> list = m_clipManager->documentClipList();
1112 //TODO: Also move ladspa effects files
1113 for (int i = 0; i < list.count(); i++) {
1114 DocClipBase *clip = list.at(i);
1115 if (clip->clipType() == TEXT) {
1116 // the image for title clip must be moved
1117 KUrl oldUrl = clip->fileURL();
1118 KUrl newUrl = KUrl(url.path() + "/titles/" + oldUrl.fileName());
1119 KIO::Job *job = KIO::copy(oldUrl, newUrl);
1120 if (KIO::NetAccess::synchronousRun(job, 0)) clip->setProperty("resource", newUrl.path());
1122 QString hash = clip->getClipHash();
1123 KUrl oldVideoThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".png");
1124 KUrl oldAudioThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".thumb");
1125 if (KIO::NetAccess::exists(oldVideoThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1126 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".png");
1127 KIO::Job *job = KIO::copy(oldVideoThumbUrl, newUrl);
1128 KIO::NetAccess::synchronousRun(job, 0);
1130 if (KIO::NetAccess::exists(oldAudioThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1131 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".thumb");
1132 KIO::Job *job = KIO::copy(oldAudioThumbUrl, newUrl);
1133 if (KIO::NetAccess::synchronousRun(job, 0)) clip->refreshThumbUrl();
1138 const QString &KdenliveDoc::profilePath() const
1140 return m_profile.path;
1143 MltVideoProfile KdenliveDoc::mltProfile() const
1148 void KdenliveDoc::setProfilePath(QString path)
1150 if (path.isEmpty()) path = KdenliveSettings::default_profile();
1151 if (path.isEmpty()) path = "dv_pal";
1152 m_profile = ProfilesDialog::getVideoProfile(path);
1153 KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
1154 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
1155 KdenliveSettings::setProject_fps(m_fps);
1156 m_width = m_profile.width;
1157 m_height = m_profile.height;
1158 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
1159 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
1160 else m_timecode.setFormat((int) m_fps);
1163 double KdenliveDoc::dar()
1165 return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
1168 void KdenliveDoc::setThumbsProgress(const QString &message, int progress)
1170 emit progressInfo(message, progress);
1173 void KdenliveDoc::loadingProgressed()
1175 m_documentLoadingProgress += m_documentLoadingStep;
1176 emit progressInfo(QString(), (int) m_documentLoadingProgress);
1179 QUndoStack *KdenliveDoc::commandStack()
1181 return m_commandStack;
1185 void KdenliveDoc::setRenderer(Render *render) {
1186 if (m_render) return;
1188 emit progressInfo(i18n("Loading playlist..."), 0);
1189 //qApp->processEvents();
1191 m_render->setSceneList(m_document.toString(), m_startPos);
1192 kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
1193 checkProjectClips();
1195 emit progressInfo(QString(), -1);
1198 void KdenliveDoc::checkProjectClips()
1200 kDebug() << "+++++++++++++ + + + + CHK PCLIPS";
1201 if (m_render == NULL) return;
1202 m_clipManager->resetProducersList(m_render->producersList());
1206 QList <Mlt::Producer *> prods = m_render->producersList();
1210 for (int i = 0; i < prods.count(); i++) {
1211 id = prods.at(i)->get("id");
1212 prodId = id.section('_', 0, 0);
1213 prodTrack = id.section('_', 1, 1);
1214 DocClipBase *clip = m_clipManager->getClipById(prodId);
1215 if (clip) clip->setProducer(prods.at(i));
1216 if (clip && clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
1217 // regenerate text clip image if required
1218 //kDebug() << "// TITLE: " << clip->getProperty("titlename") << " Preview file: " << clip->getProperty("resource") << " DOES NOT EXIST";
1219 QString titlename = clip->getProperty("name");
1220 QString titleresource;
1221 if (titlename.isEmpty()) {
1222 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1223 titlename = titleInfo.at(0);
1224 titleresource = titleInfo.at(1);
1225 clip->setProperty("name", titlename);
1226 kDebug() << "// New title set to: " << titlename;
1228 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1229 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1231 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1233 doc.setContent(clip->getProperty("xmldata"));
1234 dia_ui->setXml(doc);
1235 QImage pix = dia_ui->renderedPixmap();
1236 pix.save(titleresource);
1237 clip->setProperty("resource", titleresource);
1239 clip->producer()->set("force_reload", 1);
1244 void KdenliveDoc::updatePreviewSettings()
1246 m_clipManager->updatePreviewSettings();
1247 m_render->updatePreviewSettings();
1248 m_clipManager->resetProducersList(m_render->producersList());
1252 Render *KdenliveDoc::renderer()
1257 void KdenliveDoc::updateClip(const QString &id)
1259 emit updateClipDisplay(id);
1262 int KdenliveDoc::getFramePos(QString duration)
1264 return m_timecode.getFrameCount(duration, m_fps);
1267 QString KdenliveDoc::producerName(const QString &id)
1269 QString result = "unnamed";
1270 QDomNodeList prods = producersList();
1271 int ct = prods.count();
1272 for (int i = 0; i < ct ; i++) {
1273 QDomElement e = prods.item(i).toElement();
1274 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1275 result = e.attribute("name");
1276 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
1283 QDomDocument KdenliveDoc::toXml()
1288 Timecode KdenliveDoc::timecode() const
1293 QDomNodeList KdenliveDoc::producersList()
1295 return m_document.elementsByTagName("producer");
1298 double KdenliveDoc::projectDuration() const
1301 return GenTime(m_render->getLength(), m_fps).ms() / 1000;
1306 double KdenliveDoc::fps() const
1311 int KdenliveDoc::width() const
1316 int KdenliveDoc::height() const
1321 KUrl KdenliveDoc::url() const
1326 void KdenliveDoc::setUrl(KUrl url)
1331 void KdenliveDoc::setModified(bool mod)
1333 if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
1334 m_autoSaveTimer->start(3000);
1336 if (mod == m_modified) return;
1338 emit docModified(m_modified);
1341 bool KdenliveDoc::isModified() const
1346 const QString KdenliveDoc::description() const
1348 if (m_url.isEmpty())
1349 return i18n("Untitled") + " / " + m_profile.description;
1351 return m_url.fileName() + " / " + m_profile.description;
1354 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
1356 const QString producerId = clipId.section('_', 0, 0);
1357 DocClipBase *clip = m_clipManager->getClipById(producerId);
1358 bool placeHolder = false;
1360 elem.setAttribute("id", producerId);
1361 QString path = elem.attribute("resource");
1363 if (elem.attribute("type").toInt() == SLIDESHOW) {
1364 extension = KUrl(path).fileName();
1365 path = KUrl(path).directory();
1366 } else if (elem.attribute("type").toInt() == TEXT && QFile::exists(path) == false) {
1367 kDebug() << "// TITLE: " << elem.attribute("name") << " Preview file: " << elem.attribute("resource") << " DOES NOT EXIST";
1368 QString titlename = elem.attribute("name");
1369 QString titleresource;
1370 if (titlename.isEmpty()) {
1371 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1372 titlename = titleInfo.at(0);
1373 titleresource = titleInfo.at(1);
1374 elem.setAttribute("name", titlename);
1375 kDebug() << "// New title set to: " << titlename;
1377 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1378 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1380 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1382 doc.setContent(elem.attribute("xmldata"));
1383 dia_ui->setXml(doc);
1384 QImage pix = dia_ui->renderedPixmap();
1385 pix.save(titleresource);
1386 elem.setAttribute("resource", titleresource);
1387 setNewClipResource(clipId, titleresource);
1391 if (path.isEmpty() == false && QFile::exists(path) == false && elem.attribute("type").toInt() != TEXT) {
1392 kDebug() << "// FOUNDÂ MISSING CLIP: " << path << ", TYPE: " << elem.attribute("type").toInt();
1393 const QString size = elem.attribute("file_size");
1394 const QString hash = elem.attribute("file_hash");
1396 int action = KMessageBox::No;
1397 if (!size.isEmpty() && !hash.isEmpty()) {
1398 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
1399 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")));
1401 if (elem.attribute("type").toInt() == SLIDESHOW) {
1402 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")));
1403 if (res == KMessageBox::Yes)
1404 newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
1406 // Abort project loading
1410 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")));
1411 if (res == KMessageBox::Yes)
1412 newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
1414 // Abort project loading
1419 if (action == KMessageBox::Yes) {
1420 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
1421 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
1422 if (!m_searchFolder.isEmpty()) {
1423 newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
1425 } else if (action == KMessageBox::Cancel) {
1426 m_abortLoading = true;
1428 } else if (action == KMessageBox::No) {
1429 // Keep clip as placeHolder
1432 if (!newpath.isEmpty()) {
1433 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
1434 elem.setAttribute("resource", newpath);
1435 setNewClipResource(clipId, newpath);
1439 clip = new DocClipBase(m_clipManager, elem, producerId, placeHolder);
1440 m_clipManager->addClip(clip);
1443 if (createClipItem) {
1444 emit addProjectClip(clip);
1445 qApp->processEvents();
1446 m_render->getFileProperties(clip->toXML(), clip->getId());
1451 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path)
1453 QDomNodeList prods = m_document.elementsByTagName("producer");
1454 int maxprod = prods.count();
1455 for (int i = 0; i < maxprod; i++) {
1456 QDomNode m = prods.at(i);
1457 QString prodId = m.toElement().attribute("id");
1458 if (prodId == id || prodId.startsWith(id + '_')) {
1459 QDomNodeList params = m.childNodes();
1460 for (int j = 0; j < params.count(); j++) {
1461 QDomElement e = params.item(j).toElement();
1462 if (e.attribute("name") == "resource") {
1463 e.firstChild().setNodeValue(path);
1471 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
1473 QString foundFileName;
1474 QByteArray fileData;
1475 QByteArray fileHash;
1476 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
1477 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1478 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
1479 if (file.open(QIODevice::ReadOnly)) {
1480 if (QString::number(file.size()) == matchSize) {
1482 * 1 MB = 1 second per 450 files (or faster)
1483 * 10 MB = 9 seconds per 450 files (or faster)
1485 if (file.size() > 1000000*2) {
1486 fileData = file.read(1000000);
1487 if (file.seek(file.size() - 1000000))
1488 fileData.append(file.readAll());
1490 fileData = file.readAll();
1492 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1493 if (QString(fileHash.toHex()) == matchHash)
1494 return file.fileName();
1497 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
1499 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
1500 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1501 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
1502 if (!foundFileName.isEmpty())
1505 return foundFileName;
1508 void KdenliveDoc::addClipInfo(QDomElement elem, QDomElement orig, QString clipId)
1510 DocClipBase *clip = m_clipManager->getClipById(clipId);
1512 addClip(elem, clipId, false);
1514 QMap <QString, QString> properties;
1515 QDomNamedNodeMap attributes = elem.attributes();
1517 for (int i = 0; i < attributes.count(); i++) {
1518 attrname = attributes.item(i).nodeName();
1519 if (attrname != "resource")
1520 properties.insert(attrname, attributes.item(i).nodeValue());
1521 kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1523 clip->setProperties(properties);
1524 emit addProjectClip(clip, false);
1526 if (orig != QDomElement()) {
1527 QMap<QString, QString> meta;
1528 QDomNode m = orig.firstChild();
1529 while (!m.isNull()) {
1530 QString name = m.toElement().attribute("name");
1531 if (name.startsWith("meta.attr")) {
1532 meta.insert(name.section('.', 2, 3), m.firstChild().nodeValue());
1534 m = m.nextSibling();
1536 if (!meta.isEmpty()) {
1537 if (clip == NULL) clip = m_clipManager->getClipById(clipId);
1538 if (clip) clip->setMetadata(meta);
1543 void KdenliveDoc::deleteProjectClip(QList <QString> ids)
1545 for (int i = 0; i < ids.size(); ++i) {
1546 emit deleteTimelineClip(ids.at(i));
1547 m_clipManager->slotDeleteClip(ids.at(i));
1552 void KdenliveDoc::deleteClip(const QString &clipId)
1554 emit signalDeleteProjectClip(clipId);
1555 m_clipManager->deleteClip(clipId);
1558 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId)
1560 m_clipManager->slotAddClipList(urls, group, groupId);
1561 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1566 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
1568 //kDebug() << "///////// DOCUM, ADD CLP: " << url;
1569 m_clipManager->slotAddClipFile(url, group, groupId);
1570 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1574 const QString KdenliveDoc::getFreeClipId()
1576 return QString::number(m_clipManager->getFreeClipId());
1579 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
1581 return m_clipManager->getClipById(clipId);
1584 void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId*/)
1586 QString titlesFolder = projectFolder().path() + "/titles/";
1587 KStandardDirs::makeDir(titlesFolder);
1588 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
1589 if (dia_ui->exec() == QDialog::Accepted) {
1590 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1591 QImage pix = dia_ui->renderedPixmap();
1592 pix.save(titleInfo.at(1));
1593 //dia_ui->saveTitle(path + ".kdenlivetitle");
1594 m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
1600 int KdenliveDoc::tracksCount() const
1602 return m_tracksList.count();
1605 TrackInfo KdenliveDoc::trackInfoAt(int ix) const
1607 return m_tracksList.at(ix);
1610 void KdenliveDoc::switchTrackAudio(int ix, bool hide)
1612 m_tracksList[ix].isMute = hide; // !m_tracksList.at(ix).isMute;
1615 void KdenliveDoc::switchTrackLock(int ix, bool lock)
1617 m_tracksList[ix].isLocked = lock;
1620 bool KdenliveDoc::isTrackLocked(int ix) const
1622 return m_tracksList.at(ix).isLocked;
1625 void KdenliveDoc::switchTrackVideo(int ix, bool hide)
1627 m_tracksList[ix].isBlind = hide; // !m_tracksList.at(ix).isBlind;
1630 void KdenliveDoc::insertTrack(int ix, TrackInfo type)
1632 if (ix == -1) m_tracksList << type;
1633 else m_tracksList.insert(ix, type);
1636 void KdenliveDoc::deleteTrack(int ix)
1638 m_tracksList.removeAt(ix);
1641 void KdenliveDoc::setTrackType(int ix, TrackInfo type)
1643 m_tracksList[ix].type = type.type;
1644 m_tracksList[ix].isMute = type.isMute;
1645 m_tracksList[ix].isBlind = type.isBlind;
1646 m_tracksList[ix].isLocked = type.isLocked;
1649 const QList <TrackInfo> KdenliveDoc::tracksList() const
1651 return m_tracksList;
1654 QPoint KdenliveDoc::getTracksCount() const
1658 foreach(const TrackInfo &info, m_tracksList) {
1659 if (info.type == VIDEOTRACK) video++;
1662 return QPoint(video, audio);
1665 void KdenliveDoc::cachePixmap(const QString &fileId, const QPixmap &pix) const
1667 pix.save(m_projectFolder.path() + "/thumbs/" + fileId + ".png");
1670 QString KdenliveDoc::getLadspaFile() const
1673 QString counter = QString::number(ct).rightJustified(5, '0', false);
1674 while (QFile::exists(m_projectFolder.path() + "/ladspa/" + counter + ".ladspa")) {
1676 counter = QString::number(ct).rightJustified(5, '0', false);
1678 return m_projectFolder.path() + "/ladspa/" + counter + ".ladspa";
1681 #include "kdenlivedoc.moc"