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