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