]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
Various fixes to improve general stability in Qt 4.5.2
[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     m_view.setupUi(this);
50
51     m_scene = new CustomTrackScene(doc);
52     m_trackview = new CustomTrackView(doc, m_scene, parent);
53     m_trackview->scale(1, 1);
54     m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
55     //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
56
57     m_ruler = new CustomRuler(doc->timecode(), m_trackview);
58     connect(m_ruler, SIGNAL(zoneMoved(int, int)), this, SIGNAL(zoneMoved(int, int)));
59     QHBoxLayout *layout = new QHBoxLayout;
60     layout->setContentsMargins(m_trackview->frameWidth(), 0, 0, 0);
61     layout->setSpacing(0);
62     m_view.ruler_frame->setLayout(layout);
63     layout->addWidget(m_ruler);
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
73     QToolButton *butSmall = new QToolButton(this);
74     butSmall->setIcon(KIcon("kdenlive-zoom-small"));
75     butSmall->setToolTip(i18n("Smaller tracks"));
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     connect(butLarge, SIGNAL(clicked()), this, SLOT(slotVerticalZoomUp()));
83     sizeLayout->addWidget(butLarge);
84     m_view.size_frame->setStyleSheet(style1);
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     connect(m_view.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)), m_view.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     kDebug() << "--------- - - - -REBUILD TLK HEAD";
447     const QList <TrackInfo> list = m_doc->tracksList();
448     QLayoutItem *child;
449     m_view.headers_container->hide();
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
457     for (int i = 0; i < max; i++) {
458         HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), height, this);
459         connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
460         connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
461         connect(header, SIGNAL(switchTrackLock(int)), m_trackview, SLOT(slotSwitchTrackLock(int)));
462
463         connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
464         connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
465         connect(header, SIGNAL(changeTrack(int)), this, SIGNAL(changeTrack(int)));
466         connect(header, SIGNAL(renameTrack(int)), this, SLOT(slotRenameTrack(int)));
467         m_headersLayout->addWidget(header);
468     }
469     m_view.headers_container->show();
470 }
471
472
473 void TrackView::adjustTrackHeaders()
474 {
475     int height = KdenliveSettings::trackheight() * m_scene->scale().y();
476     QLayoutItem *child;
477     for (int i = 0; i < m_headersLayout->count(); i++) {
478         child = m_headersLayout->itemAt(i);
479         if (child->widget())(static_cast <HeaderTrack *>(child->widget()))->adjustSize(height);
480     }
481 }
482
483 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool locked)
484 {
485     // parse track
486     int position = 0;
487     QDomNodeList children = xml.childNodes();
488     for (int nodeindex = 0; nodeindex < children.count(); nodeindex++) {
489         QDomNode n = children.item(nodeindex);
490         QDomElement elem = n.toElement();
491         if (elem.tagName() == "blank") {
492             position += elem.attribute("length").toInt();
493         } else if (elem.tagName() == "entry") {
494             m_doc->loadingProgressed();
495             qApp->processEvents();
496             // Found a clip
497             int in = elem.attribute("in").toInt();
498             int out = elem.attribute("out").toInt();
499             if (in > out || /*in == out ||*/ m_invalidProducers.contains(elem.attribute("producer"))) {
500                 m_documentErrors.append(i18n("Invalid clip removed from track %1 at %2\n", ix, position));
501                 xml.removeChild(children.at(nodeindex));
502                 nodeindex--;
503                 continue;
504             }
505             QString idString = elem.attribute("producer");
506             QString id = idString;
507             double speed = 1.0;
508             int strobe = 1;
509             if (idString.startsWith("slowmotion")) {
510                 id = idString.section(':', 1, 1);
511                 speed = idString.section(':', 2, 2).toDouble();
512                 strobe = idString.section(':', 3, 3).toInt();
513                 if (strobe == 0) strobe = 1;
514             } else id = id.section('_', 0, 0);
515             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
516             if (clip == NULL) {
517                 // The clip in playlist was not listed in the kdenlive producers,
518                 // something went wrong, repair required.
519                 kWarning() << "CANNOT INSERT CLIP " << id;
520
521                 clip = getMissingProducer(id);
522                 if (!clip) {
523                     // We cannot find the producer, something is really wrong, add
524                     // placeholder color clip
525                     QDomDocument doc;
526                     QDomElement producerXml = doc.createElement("producer");
527                     doc.appendChild(producerXml);
528                     producerXml.setAttribute("colour", "0xff0000ff");
529                     producerXml.setAttribute("mlt_service", "colour");
530                     producerXml.setAttribute("length", "15000");
531                     producerXml.setAttribute("name", "INVALID");
532                     producerXml.setAttribute("type", COLOR);
533                     producerXml.setAttribute("id", id);
534                     clip = new DocClipBase(m_doc->clipManager(), doc.documentElement(), id);
535                     xml.insertBefore(producerXml, QDomNode());
536                     m_doc->clipManager()->addClip(clip);
537
538                     m_documentErrors.append(i18n("Broken clip producer %1", id) + '\n');
539                 } else {
540                     // Found correct producer
541                     m_documentErrors.append(i18n("Replaced wrong clip producer %1 with %2", id, clip->getId()) + '\n');
542                     elem.setAttribute("producer", clip->getId());
543                 }
544                 m_doc->setModified(true);
545             }
546
547             if (clip != NULL) {
548                 ItemInfo clipinfo;
549                 clipinfo.startPos = GenTime(position, m_doc->fps());
550                 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
551                 clipinfo.cropStart = GenTime(in, m_doc->fps());
552                 clipinfo.track = ix;
553                 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
554                 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), speed, strobe, false);
555                 if (idString.endsWith("_video")) item->setVideoOnly(true);
556                 else if (idString.endsWith("_audio")) item->setAudioOnly(true);
557                 m_scene->addItem(item);
558                 if (locked) item->setItemLocked(true);
559                 clip->addReference();
560                 position += (out - in + 1);
561                 kDebug() << "/////////\n\n\n" << "CLIP SPEED: " << speed << ", " << strobe << "\n\n\n/////////////////////";
562                 if (speed != 1.0 || strobe > 1) {
563                     QDomElement speedeffect = MainWindow::videoEffects.getEffectByTag(QString(), "speed").cloneNode().toElement();
564                     EffectsList::setParameter(speedeffect, "speed", QString::number((int)(100 * speed + 0.5)));
565                     EffectsList::setParameter(speedeffect, "strobe", QString::number(strobe));
566                     item->addEffect(speedeffect, false);
567                     item->effectsCounter();
568                 }
569
570                 // parse clip effects
571                 QDomNodeList effects = elem.childNodes();
572                 for (int ix = 0; ix < effects.count(); ix++) {
573                     QDomElement effect = effects.at(ix).toElement();
574                     if (effect.tagName() == "filter") {
575                         // add effect to clip
576                         QString effecttag;
577                         QString effectid;
578                         QString effectindex;
579                         QString ladspaEffectFile;
580                         // Get effect tag & index
581                         for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
582                             // parse effect parameters
583                             QDomElement effectparam = n3.toElement();
584                             if (effectparam.attribute("name") == "tag") {
585                                 effecttag = effectparam.text();
586                             } else if (effectparam.attribute("name") == "kdenlive_id") {
587                                 effectid = effectparam.text();
588                             } else if (effectparam.attribute("name") == "kdenlive_ix") {
589                                 effectindex = effectparam.text();
590                             } else if (effectparam.attribute("name") == "src") {
591                                 ladspaEffectFile = effectparam.text();
592                                 if (!QFile::exists(ladspaEffectFile)) {
593                                     // If the ladspa effect file is missing, recreate it
594                                     kDebug() << "// MISSING LADSPA FILE: " << ladspaEffectFile;
595                                     ladspaEffectFile = m_doc->getLadspaFile();
596                                     effectparam.firstChild().setNodeValue(ladspaEffectFile);
597                                     kDebug() << "// ... REPLACED WITH: " << ladspaEffectFile;
598                                 }
599                             }
600                         }
601                         //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
602                         // get effect standard tags
603                         QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
604                         if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
605                         if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
606                         if (clipeffect.isNull()) {
607                             kDebug() << "///  WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
608                             m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
609                             elem.removeChild(effects.at(ix));
610                             ix--;
611                         } else {
612                             QDomElement currenteffect = clipeffect.cloneNode().toElement();
613                             currenteffect.setAttribute("kdenlive_ix", effectindex);
614                             QDomNodeList clipeffectparams = currenteffect.childNodes();
615
616                             if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
617                                 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND  * * * * * * * * * * *";
618                                 // effect is key-framable, read all effects to retrieve keyframes
619                                 QString factor;
620                                 QString starttag;
621                                 QString endtag;
622                                 QDomNodeList params = currenteffect.elementsByTagName("parameter");
623                                 for (int i = 0; i < params.count(); i++) {
624                                     QDomElement e = params.item(i).toElement();
625                                     if (e.attribute("type") == "keyframe") {
626                                         starttag = e.attribute("starttag", "start");
627                                         endtag = e.attribute("endtag", "end");
628                                         factor = e.attribute("factor", "1");
629                                         break;
630                                     }
631                                 }
632                                 QString keyframes;
633                                 int effectin = effect.attribute("in").toInt();
634                                 int effectout = effect.attribute("out").toInt();
635                                 double startvalue = 0;
636                                 double endvalue = 0;
637                                 double fact;
638                                 if (factor.isEmpty()) fact = 1;
639                                 else if (factor.startsWith('%')) {
640                                     fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
641                                 } else fact = factor.toDouble();
642                                 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
643                                     // parse effect parameters
644                                     QDomElement effectparam = n3.toElement();
645                                     if (effectparam.attribute("name") == starttag)
646                                         startvalue = effectparam.text().toDouble() * fact;
647                                     if (effectparam.attribute("name") == endtag)
648                                         endvalue = effectparam.text().toDouble() * fact;
649                                 }
650                                 // add first keyframe
651                                 keyframes.append(QString::number(effectin) + ':' + QString::number(startvalue) + ';' + QString::number(effectout) + ':' + QString::number(endvalue) + ';');
652                                 QDomNode lastParsedEffect;
653                                 ix++;
654                                 QDomNode n2 = effects.at(ix);
655                                 bool continueParsing = true;
656                                 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
657                                     // parse all effects
658                                     QDomElement kfreffect = n2.toElement();
659                                     int effectout = kfreffect.attribute("out").toInt();
660
661                                     for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
662                                         // parse effect parameters
663                                         QDomElement subeffectparam = n4.toElement();
664                                         if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
665                                             //We are not in the same effect, stop parsing
666                                             lastParsedEffect = n2.previousSibling();
667                                             ix--;
668                                             continueParsing = false;
669                                             break;
670                                         } else if (subeffectparam.attribute("name") == endtag) {
671                                             endvalue = subeffectparam.text().toDouble() * fact;
672                                             break;
673                                         }
674                                     }
675                                     if (continueParsing) {
676                                         keyframes.append(QString::number(effectout) + ':' + QString::number(endvalue) + ';');
677                                         ix++;
678                                     }
679                                 }
680
681                                 params = currenteffect.elementsByTagName("parameter");
682                                 for (int i = 0; i < params.count(); i++) {
683                                     QDomElement e = params.item(i).toElement();
684                                     if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
685                                 }
686                                 if (!continueParsing) {
687                                     n2 = lastParsedEffect;
688                                 }
689                             } else {
690                                 // Check if effect has in/out points
691                                 if (effect.hasAttribute("in")) {
692                                     EffectsList::setParameter(currenteffect, "in",  effect.attribute("in"));
693                                 }
694                                 if (effect.hasAttribute("out")) {
695                                     EffectsList::setParameter(currenteffect, "out",  effect.attribute("out"));
696                                 }
697                             }
698
699                             // adjust effect parameters
700                             for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
701                                 // parse effect parameters
702                                 QDomElement effectparam = n3.toElement();
703                                 QString paramname = effectparam.attribute("name");
704                                 QString paramvalue = effectparam.text();
705
706
707                                 // try to find this parameter in the effect xml
708                                 QDomElement e;
709                                 for (int k = 0; k < clipeffectparams.count(); k++) {
710                                     e = clipeffectparams.item(k).toElement();
711                                     if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
712                                         if (e.attribute("factor", "1") != "1") {
713                                             QString factor = e.attribute("factor", "1");
714                                             double fact;
715                                             if (factor.startsWith('%')) {
716                                                 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
717                                             } else fact = factor.toDouble();
718                                             e.setAttribute("value", paramvalue.toDouble() * fact);
719                                         } else e.setAttribute("value", paramvalue);
720                                         break;
721                                     }
722                                 }
723                             }
724                             if (effecttag == "ladspa") {
725                                 //QString ladspaEffectFile = EffectsList::parameter(effect, "src", "property");
726
727                                 if (!QFile::exists(ladspaEffectFile)) {
728                                     // If the ladspa effect file is missing, recreate it
729                                     initEffects::ladspaEffectFile(ladspaEffectFile, currenteffect.attribute("ladspaid").toInt(), m_trackview->getLadspaParams(currenteffect));
730                                 }
731                                 currenteffect.setAttribute("src", ladspaEffectFile);
732                             }
733                             item->addEffect(currenteffect, false);
734                             item->effectsCounter();
735                         }
736                     }
737                 }
738             }
739             //m_clipList.append(clip);
740         }
741     }
742     //m_trackDuration = position;
743
744
745     //documentTracks.insert(ix, track);
746     kDebug() << "*************  ADD DOC TRACK " << ix << ", DURATION: " << position;
747     return position;
748     //track->show();
749 }
750
751 DocClipBase *TrackView::getMissingProducer(const QString id) const
752 {
753     QDomElement missingXml;
754     QDomDocument doc = m_doc->toXml();
755     QString docRoot = doc.documentElement().attribute("root");
756     if (!docRoot.endsWith('/')) docRoot.append('/');
757     QDomNodeList prods = doc.elementsByTagName("producer");
758     int maxprod = prods.count();
759     for (int i = 0; i < maxprod; i++) {
760         QDomNode m = prods.at(i);
761         QString prodId = m.toElement().attribute("id");
762         if (prodId == id) {
763             missingXml =  m.toElement();
764             break;
765         }
766     }
767     if (missingXml == QDomElement()) return NULL;
768
769     QDomNodeList params = missingXml.childNodes();
770     QString resource;
771     for (int j = 0; j < params.count(); j++) {
772         QDomElement e = params.item(j).toElement();
773         if (e.attribute("name") == "resource") {
774             resource = e.firstChild().nodeValue();
775             break;
776         }
777     }
778     // prepend MLT XML document root if no path in clip resource and not a color clip
779     if (!resource.startsWith('/') && !resource.startsWith("0x")) resource.prepend(docRoot);
780     DocClipBase *missingClip = NULL;
781     if (!resource.isEmpty())
782         missingClip = m_doc->clipManager()->getClipByResource(resource);
783     return missingClip;
784 }
785
786 QGraphicsScene *TrackView::projectScene()
787 {
788     return m_scene;
789 }
790
791 CustomTrackView *TrackView::projectView()
792 {
793     return m_trackview;
794 }
795
796 void TrackView::setEditMode(const QString & editMode)
797 {
798     m_editMode = editMode;
799 }
800
801 const QString & TrackView::editMode() const
802 {
803     return m_editMode;
804 }
805
806 void TrackView::slotChangeTrackLock(int ix, bool lock)
807 {
808     QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
809     widgets.at(ix)->setLock(lock);
810 }
811
812 void TrackView::slotVerticalZoomDown()
813 {
814     if (m_verticalZoom == 0) return;
815     m_verticalZoom--;
816     m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
817     if (m_verticalZoom == 0) m_trackview->setScale(m_scene->scale().x(), 0.5);
818     else m_trackview->setScale(m_scene->scale().x(), 1);
819     adjustTrackHeaders();
820     /*KdenliveSettings::setTrackheight(KdenliveSettings::trackheight() / 2);
821     m_trackview->checkTrackHeight(false);*/
822 }
823
824 void TrackView::slotVerticalZoomUp()
825 {
826     if (m_verticalZoom == 2) return;
827     m_verticalZoom++;
828     m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
829     /*KdenliveSettings::setTrackheight(KdenliveSettings::trackheight() * 2);
830     m_trackview->checkTrackHeight(false);*/
831     if (m_verticalZoom == 2) m_trackview->setScale(m_scene->scale().x(), 2);
832     else m_trackview->setScale(m_scene->scale().x(), 1);
833     adjustTrackHeaders();
834 }
835
836 void TrackView::updateProjectFps()
837 {
838     m_ruler->updateProjectFps(m_doc->timecode());
839 }
840
841 void TrackView::slotRenameTrack(int ix)
842 {
843     int tracknumber = m_doc->tracksCount() - ix;
844     TrackInfo info = m_doc->trackInfoAt(tracknumber - 1);
845     bool ok;
846     QString newName = QInputDialog::getText(this, i18n("New Track Name"), i18n("Enter new name"), QLineEdit::Normal, info.trackName, &ok);
847     if (ok) {
848         info.trackName = newName;
849         m_doc->setTrackType(tracknumber - 1, info);
850         QTimer::singleShot(300, this, SLOT(slotRebuildTrackHeaders()));
851         m_doc->setModified(true);
852     }
853 }
854
855
856 #include "trackview.moc"