]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
First steps for "split audio" feature,. Don't use yet, it might corrupt your projects
[kdenlive] / src / trackview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "trackview.h"
22 #include "definitions.h"
23 #include "headertrack.h"
24 #include "clipitem.h"
25 #include "transition.h"
26 #include "kdenlivesettings.h"
27 #include "clipmanager.h"
28 #include "customruler.h"
29 #include "kdenlivedoc.h"
30 #include "mainwindow.h"
31 #include "customtrackview.h"
32 #include "initeffects.h"
33
34 #include <KDebug>
35 #include <KMessageBox>
36
37 #include <QScrollBar>
38
39 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
40         : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0) {
41
42     view = new Ui::TimeLine_UI();
43     view->setupUi(this);
44
45     m_scene = new CustomTrackScene(doc);
46     m_trackview = new CustomTrackView(doc, m_scene, parent);
47     m_trackview->scale(1, 1);
48     m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
49     //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
50
51     m_ruler = new CustomRuler(doc->timecode(), m_trackview);
52     connect(m_ruler, SIGNAL(zoneMoved(int, int)), this, SIGNAL(zoneMoved(int, int)));
53     QHBoxLayout *layout = new QHBoxLayout;
54     view->ruler_frame->setLayout(layout);
55     int left_margin;
56     int right_margin;
57     layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
58     layout->setContentsMargins(left_margin, 0, right_margin, 0);
59     layout->addWidget(m_ruler);
60
61     QHBoxLayout *tracksLayout = new QHBoxLayout;
62     tracksLayout->setContentsMargins(0, 0, 0, 0);
63     view->tracks_frame->setLayout(tracksLayout);
64
65     view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66     view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67
68     m_headersLayout = new QVBoxLayout;
69     m_headersLayout->setContentsMargins(0, 0, 0, 0);
70     m_headersLayout->setSpacing(0);
71     view->headers_container->setLayout(m_headersLayout);
72
73     connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
74
75     tracksLayout->addWidget(m_trackview);
76
77     connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
78     connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
79
80     parseDocument(m_doc->toXml());
81     m_doc->setSceneList();
82     connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
83     connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
84     connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
85     connect(m_trackview, SIGNAL(transitionItemSelected(Transition*, bool)), this, SLOT(slotTransitionItemSelected(Transition*, bool)));
86     connect(m_trackview, SIGNAL(doTrackLock(int, bool)), this, SLOT(slotChangeTrackLock(int, bool)));
87
88     slotChangeZoom(m_doc->zoom());
89     slotSetZone(m_doc->zone());
90 }
91
92
93 int TrackView::duration() const {
94     return m_trackview->duration();
95 }
96
97 int TrackView::tracksNumber() const {
98     return m_projectTracks - 1;
99 }
100
101 int TrackView::inPoint() const {
102     return m_ruler->inPoint();
103 }
104
105 int TrackView::outPoint() const {
106     return m_ruler->outPoint();
107 }
108
109 void TrackView::slotSetZone(QPoint p) {
110     m_ruler->setZone(p);
111 }
112
113 void TrackView::slotTransitionItemSelected(Transition *t, bool update) {
114     emit transitionItemSelected(t, update);
115 }
116
117 void TrackView::setDuration(int dur) {
118     m_trackview->setDuration(dur);
119     m_ruler->setDuration(dur);
120 }
121
122 void TrackView::parseDocument(QDomDocument doc) {
123     //int cursorPos = 0;
124     m_documentErrors.clear();
125     // kDebug() << "//// DOCUMENT: " << doc.toString();
126     /*QDomNode props = doc.elementsByTagName("properties").item(0);
127     if (!props.isNull()) {
128         cursorPos = props.toElement().attribute("timeline_position").toInt();
129     }*/
130
131     // parse project tracks
132     QDomElement tractor = doc.elementsByTagName("tractor").item(0).toElement();
133     QDomNodeList tracks = doc.elementsByTagName("track");
134     QDomNodeList playlists = doc.elementsByTagName("playlist");
135     int duration = 300;
136     m_projectTracks = tracks.count();
137     int trackduration = 0;
138     QDomElement e;
139     QDomElement p;
140
141     int pos = m_projectTracks - 1;
142     m_invalidProducers.clear();
143     QDomNodeList producers = doc.elementsByTagName("producer");
144     for (int i = 0; i < producers.count(); i++) {
145         // CHeck for invalid producers
146         QDomNode n = producers.item(i);
147         e = n.toElement();
148         int in = e.attribute("in").toInt();
149         int out = e.attribute("out").toInt();
150         if (in > out || in == out) {
151             // invalid producer, remove it
152             QString id = e.attribute("id");
153             m_invalidProducers.append(id);
154             m_documentErrors.append(i18n("Invalid clip producer %1\n", id));
155             doc.documentElement().removeChild(producers.at(i));
156             i--;
157         }
158     }
159
160     for (int i = 0; i < m_projectTracks; i++) {
161         e = tracks.item(i).toElement();
162         QString playlist_name = e.attribute("producer");
163         if (playlist_name != "black_track" && playlist_name != "playlistmain") {
164             // find playlist related to this track
165             p = QDomElement();
166             for (int j = 0; j < m_projectTracks; j++) {
167                 p = playlists.item(j).toElement();
168                 if (p.attribute("id") == playlist_name) break;
169             }
170             if (p.attribute("id") != playlist_name) { // then it didn't work.
171                 kDebug() << "NO PLAYLIST FOUND FOR TRACK " + pos;
172             }
173             if (e.attribute("hide") == "video") {
174                 m_doc->switchTrackVideo(i - 1, true);
175             } else if (e.attribute("hide") == "audio") {
176                 m_doc->switchTrackAudio(i - 1, true);
177             } else if (e.attribute("hide") == "both") {
178                 m_doc->switchTrackVideo(i - 1, true);
179                 m_doc->switchTrackAudio(i - 1, true);
180             }
181             trackduration = slotAddProjectTrack(pos, p, m_doc->isTrackLocked(i - 1));
182             pos--;
183             //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
184             if (trackduration > duration) duration = trackduration;
185         } else {
186             // background black track
187             for (int j = 0; j < m_projectTracks; j++) {
188                 p = playlists.item(j).toElement();
189                 if (p.attribute("id") == playlist_name) break;
190             }
191             int black_clips = p.childNodes().count();
192             for (int i = 0; i < black_clips; i++)
193                 m_doc->loadingProgressed();
194             qApp->processEvents();
195             pos--;
196         }
197     }
198
199     // parse transitions
200     QDomNodeList transitions = doc.elementsByTagName("transition");
201
202     //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
203     for (int i = 0; i < transitions.count(); i++) {
204         e = transitions.item(i).toElement();
205         QDomNodeList transitionparams = e.childNodes();
206         bool transitionAdd = true;
207         int a_track = 0;
208         int b_track = 0;
209         bool isAutomatic = false;
210         bool forceTrack = false;
211         QString mlt_geometry;
212         QString mlt_service;
213         for (int k = 0; k < transitionparams.count(); k++) {
214             p = transitionparams.item(k).toElement();
215             if (!p.isNull()) {
216                 QString paramName = p.attribute("name");
217                 // do not add audio mixing transitions
218                 if (paramName == "internal_added" && p.text() == "237") {
219                     transitionAdd = false;
220                     //kDebug() << "//  TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
221                     //break;
222                 } else if (paramName == "a_track") a_track = p.text().toInt();
223                 else if (paramName == "b_track") b_track = p.text().toInt();
224                 else if (paramName == "mlt_service") mlt_service = p.text();
225                 else if (paramName == "geometry") mlt_geometry = p.text();
226                 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
227                 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
228             }
229         }
230         if (transitionAdd || mlt_service != "mix") {
231             // Transition should be added to the scene
232             ItemInfo transitionInfo;
233             QString transitionId;
234             if (mlt_service == "composite") {
235                 // When adding composite transition, check if it is a wipe transition
236                 if (mlt_geometry.count(';') == 1) {
237                     mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
238                     mlt_geometry.replace(QChar('x'), QChar(','), Qt::CaseInsensitive);
239                     QString start = mlt_geometry.section(';', 0, 0);
240                     start = start.section(':', 0, 1);
241                     start.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
242                     QString end = mlt_geometry.section('=', 1, 1);
243                     end = end.section(':', 0, 1);
244                     end.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
245                     start.append(',' + end);
246                     QStringList numbers = start.split(',', QString::SkipEmptyParts);
247                     bool isWipeTransition = true;
248                     int checkNumber;
249                     for (int i = 0; i < numbers.size(); ++i) {
250                         checkNumber = qAbs(numbers.at(i).toInt());
251                         if (checkNumber != 0 && checkNumber != 100) {
252                             isWipeTransition = false;
253                             break;
254                         }
255                     }
256                     if (isWipeTransition) transitionId = "wipe";
257                 }
258             }
259             QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
260
261             for (int k = 0; k < transitionparams.count(); k++) {
262                 p = transitionparams.item(k).toElement();
263                 if (!p.isNull()) {
264                     QString paramName = p.attribute("name");
265                     QString paramValue = p.text();
266
267                     QDomNodeList params = base.elementsByTagName("parameter");
268                     if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
269                             QDomElement e = params.item(i).toElement();
270                             if (!e.isNull() && e.attribute("tag") == paramName) {
271                                 if (e.attribute("type") == "double") {
272                                     QString factor = e.attribute("factor", "1");
273                                     if (factor != "1") {
274                                         double val = paramValue.toDouble() * factor.toDouble();
275                                         paramValue = QString::number(val);
276                                     }
277                                 }
278                                 e.setAttribute("value", paramValue);
279                                 break;
280                             }
281                         }
282                 }
283             }
284
285             /*QDomDocument doc;
286             doc.appendChild(doc.importNode(base, true));
287             kDebug() << "///////  TRANSITION XML: "<< doc.toString();*/
288
289             transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
290             transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
291             transitionInfo.track = m_projectTracks - 1 - b_track;
292             //kDebug() << "///////////////   +++++++++++  ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
293             if (transitionInfo.startPos >= transitionInfo.endPos) {
294                 // invalid transition, remove it.
295                 m_documentErrors.append(i18n("Removed invalid transition: %1", e.attribute("id")) + '\n');
296                 kDebug() << "///// REMOVED INVALID TRANSITION: " << e.attribute("id");
297                 tractor.removeChild(transitions.item(i));
298                 i--;
299             } else {
300                 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
301                 if (forceTrack) tr->setForcedTrack(true, a_track);
302                 m_scene->addItem(tr);
303                 if (m_doc->isTrackLocked(b_track - 1)) {
304                     tr->setItemLocked(true);
305                 }
306             }
307         }
308     }
309
310     // Add guides
311     QDomNodeList guides = doc.elementsByTagName("guide");
312     for (int i = 0; i < guides.count(); i++) {
313         e = guides.item(i).toElement();
314         const QString comment = e.attribute("comment");
315         const GenTime pos = GenTime(e.attribute("time").toDouble());
316         m_trackview->addGuide(pos, comment);
317     }
318
319     // Rebuild groups
320     QDomNodeList groups = doc.elementsByTagName("group");
321     m_trackview->loadGroups(groups);
322
323     m_trackview->setDuration(duration);
324     kDebug() << "///////////  TOTAL PROJECT DURATION: " << duration;
325     slotRebuildTrackHeaders();
326     if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
327     //m_trackview->setCursorPos(cursorPos);
328     //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
329 }
330
331 void TrackView::slotDeleteClip(const QString &clipId) {
332     m_trackview->deleteClip(clipId);
333 }
334
335 void TrackView::setCursorPos(int pos) {
336     m_trackview->setCursorPos(pos);
337 }
338
339 void TrackView::moveCursorPos(int pos) {
340     m_trackview->setCursorPos(pos, false);
341 }
342
343 void TrackView::slotChangeZoom(int factor) {
344     m_doc->setZoom(factor);
345     m_ruler->setPixelPerMark(factor);
346     m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
347     m_trackview->setScale(m_scale);
348 }
349
350 int TrackView::fitZoom() const {
351     int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
352     int i;
353     for (i = 0; i < 13; i++)
354         if (m_ruler->comboScale[i] > zoom) break;
355     return i;
356 }
357
358 KdenliveDoc *TrackView::document() {
359     return m_doc;
360 }
361
362 void TrackView::refresh() {
363     m_trackview->viewport()->update();
364 }
365
366 void TrackView::slotRebuildTrackHeaders() {
367     QList <TrackInfo> list = m_doc->tracksList();
368     QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
369     for (int i = 0; i < widgets.count(); i++)
370         delete widgets.at(i);
371     int max = list.count();
372     for (int i = 0; i < max; i++) {
373         HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
374         connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
375         connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
376         connect(header, SIGNAL(switchTrackLock(int)), m_trackview, SLOT(slotSwitchTrackLock(int)));
377
378         connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
379         connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
380         connect(header, SIGNAL(changeTrack(int)), this, SIGNAL(changeTrack(int)));
381         m_headersLayout->addWidget(header);
382     }
383     view->headers_container->adjustSize();
384 }
385
386
387 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool locked) {
388     // parse track
389     int position = 0;
390     QDomNodeList children = xml.childNodes();
391     for (int nodeindex = 0; nodeindex < children.count(); nodeindex++) {
392         QDomNode n = children.item(nodeindex);
393         QDomElement elem = n.toElement();
394         if (elem.tagName() == "blank") {
395             position += elem.attribute("length").toInt();
396         } else if (elem.tagName() == "entry") {
397             m_doc->loadingProgressed();
398             qApp->processEvents();
399             // Found a clip
400             int in = elem.attribute("in").toInt();
401             int out = elem.attribute("out").toInt();
402             if (in > out || in == out || m_invalidProducers.contains(elem.attribute("producer"))) {
403                 m_documentErrors.append(i18n("Invalid clip removed from track %1 at %2\n", ix, position));
404                 xml.removeChild(children.at(nodeindex));
405                 nodeindex--;
406                 continue;
407             }
408             QString idString = elem.attribute("producer");
409             QString id = idString;
410             double speed = 1.0;
411             if (idString.startsWith("slowmotion")) {
412                 id = idString.section(':', 1, 1);
413                 speed = idString.section(':', 2, 2).toDouble();
414             } else id = id.section('_', 0, 0);
415             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
416             if (clip != NULL) {
417                 ItemInfo clipinfo;
418                 clipinfo.startPos = GenTime(position, m_doc->fps());
419                 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
420                 clipinfo.cropStart = GenTime(in, m_doc->fps());
421                 clipinfo.track = ix;
422                 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
423                 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), speed, false);
424                 if (idString.endsWith("_video")) item->setVideoOnly(true);
425                 else if (idString.endsWith("_audio")) item->setAudioOnly(true);
426                 m_scene->addItem(item);
427                 if (locked) item->setItemLocked(true);
428                 clip->addReference();
429                 position += (out - in + 1);
430
431                 // parse clip effects
432                 QDomNodeList effects = elem.childNodes();
433                 for (int ix = 0; ix < effects.count(); ix++) {
434                     QDomElement effect = effects.at(ix).toElement();
435                     if (effect.tagName() == "filter") {
436                         // add effect to clip
437                         QString effecttag;
438                         QString effectid;
439                         QString effectindex;
440                         QString ladspaEffectFile;
441                         // Get effect tag & index
442                         for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
443                             // parse effect parameters
444                             QDomElement effectparam = n3.toElement();
445                             if (effectparam.attribute("name") == "tag") {
446                                 effecttag = effectparam.text();
447                             } else if (effectparam.attribute("name") == "kdenlive_id") {
448                                 effectid = effectparam.text();
449                             } else if (effectparam.attribute("name") == "kdenlive_ix") {
450                                 effectindex = effectparam.text();
451                             } else if (effectparam.attribute("name") == "src") {
452                                 ladspaEffectFile = effectparam.text();
453                                 if (!QFile::exists(ladspaEffectFile)) {
454                                     // If the ladspa effect file is missing, recreate it
455                                     kDebug() << "// MISSING LADSPA FILE: " << ladspaEffectFile;
456                                     ladspaEffectFile = m_doc->getLadspaFile();
457                                     effectparam.firstChild().setNodeValue(ladspaEffectFile);
458                                     kDebug() << "// ... REPLACED WITH: " << ladspaEffectFile;
459                                 }
460                             }
461                         }
462                         //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
463                         // get effect standard tags
464                         QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
465                         if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
466                         if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
467                         if (clipeffect.isNull()) {
468                             kDebug() << "///  WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
469                             m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
470                             elem.removeChild(effects.at(ix));
471                             ix--;
472                         } else {
473                             QDomElement currenteffect = clipeffect.cloneNode().toElement();
474                             currenteffect.setAttribute("kdenlive_ix", effectindex);
475                             QDomNodeList clipeffectparams = currenteffect.childNodes();
476
477                             if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
478                                 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND  * * * * * * * * * * *";
479                                 // effect is key-framable, read all effects to retrieve keyframes
480                                 double factor = 0;
481                                 QString starttag;
482                                 QString endtag;
483                                 QDomNodeList params = currenteffect.elementsByTagName("parameter");
484                                 for (int i = 0; i < params.count(); i++) {
485                                     QDomElement e = params.item(i).toElement();
486                                     if (e.attribute("type") == "keyframe") {
487                                         starttag = e.attribute("starttag", "start");
488                                         endtag = e.attribute("endtag", "end");
489                                         factor = e.attribute("factor", "1").toDouble();
490                                         break;
491                                     }
492                                 }
493                                 QString keyframes;
494                                 int effectin = effect.attribute("in").toInt();
495                                 int effectout = effect.attribute("out").toInt();
496                                 double startvalue = 0;
497                                 double endvalue = 0;
498                                 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
499                                     // parse effect parameters
500                                     QDomElement effectparam = n3.toElement();
501                                     if (effectparam.attribute("name") == starttag)
502                                         startvalue = effectparam.text().toDouble() * factor;
503                                     if (effectparam.attribute("name") == endtag)
504                                         endvalue = effectparam.text().toDouble() * factor;
505                                 }
506                                 // add first keyframe
507                                 keyframes.append(QString::number(effectin) + ':' + QString::number(startvalue) + ';' + QString::number(effectout) + ':' + QString::number(endvalue) + ';');
508                                 QDomNode lastParsedEffect;
509                                 ix++;
510                                 QDomNode n2 = effects.at(ix);
511                                 bool continueParsing = true;
512                                 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
513                                     // parse all effects
514                                     QDomElement kfreffect = n2.toElement();
515                                     int effectout = kfreffect.attribute("out").toInt();
516
517                                     for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
518                                         // parse effect parameters
519                                         QDomElement subeffectparam = n4.toElement();
520                                         if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
521                                             //We are not in the same effect, stop parsing
522                                             lastParsedEffect = n2.previousSibling();
523                                             ix--;
524                                             continueParsing = false;
525                                             break;
526                                         } else if (subeffectparam.attribute("name") == endtag) {
527                                             endvalue = subeffectparam.text().toDouble() * factor;
528                                             break;
529                                         }
530                                     }
531                                     if (continueParsing) {
532                                         keyframes.append(QString::number(effectout) + ':' + QString::number(endvalue) + ';');
533                                         ix++;
534                                     }
535                                 }
536
537                                 params = currenteffect.elementsByTagName("parameter");
538                                 for (int i = 0; i < params.count(); i++) {
539                                     QDomElement e = params.item(i).toElement();
540                                     if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
541                                 }
542                                 if (!continueParsing) {
543                                     n2 = lastParsedEffect;
544                                 }
545                             } else {
546                                 // Check if effect has in/out points
547                                 if (effect.hasAttribute("in")) {
548                                     EffectsList::setParameter(currenteffect, "in",  effect.attribute("in"));
549                                 }
550                                 if (effect.hasAttribute("out")) {
551                                     EffectsList::setParameter(currenteffect, "out",  effect.attribute("out"));
552                                 }
553                             }
554
555                             // adjust effect parameters
556                             for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
557                                 // parse effect parameters
558                                 QDomElement effectparam = n3.toElement();
559                                 QString paramname = effectparam.attribute("name");
560                                 QString paramvalue = effectparam.text();
561
562
563                                 // try to find this parameter in the effect xml
564                                 QDomElement e;
565                                 for (int k = 0; k < clipeffectparams.count(); k++) {
566                                     e = clipeffectparams.item(k).toElement();
567                                     if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
568                                         double factor = e.attribute("factor", "1").toDouble();
569                                         if (factor != 1) {
570                                             e.setAttribute("value", paramvalue.toDouble() * factor);
571                                         } else e.setAttribute("value", paramvalue);
572                                         break;
573                                     }
574                                 }
575                             }
576                             if (effecttag == "ladspa") {
577                                 //QString ladspaEffectFile = EffectsList::parameter(effect, "src", "property");
578
579                                 if (!QFile::exists(ladspaEffectFile)) {
580                                     // If the ladspa effect file is missing, recreate it
581                                     initEffects::ladspaEffectFile(ladspaEffectFile, currenteffect.attribute("ladspaid").toInt(), m_trackview->getLadspaParams(currenteffect));
582                                 }
583                                 currenteffect.setAttribute("src", ladspaEffectFile);
584                             }
585                             item->addEffect(currenteffect, false);
586                             item->effectsCounter();
587                         }
588                     }
589                 }
590             } else {
591                 // The clip in playlist was not listed in the kdenlive producers,
592                 // something went wrong, repair required.
593                 kWarning() << "CANNOT INSERT CLIP " << id;
594                 int out = elem.attribute("out").toInt();
595
596                 ItemInfo clipinfo;
597                 clipinfo.startPos = GenTime(position, m_doc->fps());
598                 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
599                 clipinfo.cropStart = GenTime(in, m_doc->fps());
600                 clipinfo.track = ix;
601                 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
602
603                 DocClipBase *missingClip = getMissingProducer(id);
604                 if (!missingClip) {
605                     // We cannot find the producer, something is really wrong, add
606                     // placeholder color clip
607                     QDomElement producerXml;
608                     producerXml.setTagName("producer");
609                     producerXml.setAttribute("resource", "0xff0000ff");
610                     producerXml.setAttribute("mlt_service", "colour");
611                     producerXml.setAttribute("length", "15000");
612                     producerXml.setAttribute("id", id);
613                     missingClip = new DocClipBase(m_doc->clipManager(), producerXml, id);
614                     m_documentErrors.append(i18n("Boken clip producer %1", id) + '\n');
615                 } else m_documentErrors.append(i18n("Replaced wrong clip producer %1 with %2", id, missingClip->getId()) + '\n');
616                 ClipItem *item = new ClipItem(missingClip, clipinfo, m_doc->fps(), 1.0, false);
617                 m_scene->addItem(item);
618                 missingClip->addReference();
619                 position += (out - in + 1);
620
621                 m_doc->setModified(true);
622             }
623             //m_clipList.append(clip);
624         }
625     }
626     //m_trackDuration = position;
627
628
629     //documentTracks.insert(ix, track);
630     kDebug() << "*************  ADD DOC TRACK " << ix << ", DURATION: " << position;
631     return position;
632     //track->show();
633 }
634
635 DocClipBase *TrackView::getMissingProducer(const QString id) const {
636     QDomElement missingXml = QDomElement();
637     QDomDocument doc = m_doc->toXml();
638     QString docRoot = doc.documentElement().attribute("root");
639     if (!docRoot.endsWith('/')) docRoot.append('/');
640     QDomNodeList prods = doc.elementsByTagName("producer");
641     int maxprod = prods.count();
642     for (int i = 0; i < maxprod; i++) {
643         QDomNode m = prods.at(i);
644         QString prodId = m.toElement().attribute("id");
645         if (prodId == id) {
646             missingXml =  m.toElement();
647             break;
648         }
649     }
650     if (missingXml == QDomElement()) return NULL;
651
652     QDomNodeList params = missingXml.childNodes();
653     QString resource;
654     QString mlt_service;
655     for (int j = 0; j < params.count(); j++) {
656         QDomElement e = params.item(j).toElement();
657         if (e.attribute("name") == "resource") {
658             resource = e.firstChild().nodeValue();
659         } else if (e.attribute("name") == "mlt_service") {
660             mlt_service = e.firstChild().nodeValue();
661         }
662     }
663     resource.prepend(docRoot);
664     DocClipBase *missingClip = NULL;
665     if (!resource.isEmpty())
666         missingClip = m_doc->clipManager()->getClipByResource(resource);
667     return missingClip;
668 }
669
670 QGraphicsScene *TrackView::projectScene() {
671     return m_scene;
672 }
673
674 CustomTrackView *TrackView::projectView() {
675     return m_trackview;
676 }
677
678 void TrackView::setEditMode(const QString & editMode) {
679     m_editMode = editMode;
680 }
681
682 const QString & TrackView::editMode() const {
683     return m_editMode;
684 }
685
686 void TrackView::slotChangeTrackLock(int ix, bool lock) {
687     QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
688     widgets.at(ix)->setLock(lock);
689 }
690
691
692 #include "trackview.moc"