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