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