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