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