1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
24 #include <KMessageBox>
26 #include "definitions.h"
27 #include "headertrack.h"
28 #include "trackview.h"
30 #include "transition.h"
31 #include "kdenlivesettings.h"
32 #include "clipmanager.h"
33 #include "customruler.h"
34 #include "kdenlivedoc.h"
35 #include "mainwindow.h"
36 #include "customtrackview.h"
37 #include "initeffects.h"
39 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
40 : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0) {
42 view = new Ui::TimeLine_UI();
45 m_scene = new CustomTrackScene(doc);
46 m_trackview = new CustomTrackView(doc, m_scene, parent);
47 m_trackview->scale(1, 1);
48 m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
49 //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
51 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
52 connect(m_ruler, SIGNAL(zoneMoved(int, int)), this, SIGNAL(zoneMoved(int, int)));
53 QHBoxLayout *layout = new QHBoxLayout;
54 view->ruler_frame->setLayout(layout);
57 layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
58 layout->setContentsMargins(left_margin, 0, right_margin, 0);
59 layout->addWidget(m_ruler);
61 QHBoxLayout *tracksLayout = new QHBoxLayout;
62 tracksLayout->setContentsMargins(0, 0, 0, 0);
63 view->tracks_frame->setLayout(tracksLayout);
65 view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
68 m_headersLayout = new QVBoxLayout;
69 m_headersLayout->setContentsMargins(0, 0, 0, 0);
70 m_headersLayout->setSpacing(0);
71 view->headers_container->setLayout(m_headersLayout);
73 connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
75 tracksLayout->addWidget(m_trackview);
77 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
78 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
80 parseDocument(m_doc->toXml());
81 m_doc->setSceneList();
82 connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
83 connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
84 connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
85 connect(m_trackview, SIGNAL(transitionItemSelected(Transition*, bool)), this, SLOT(slotTransitionItemSelected(Transition*, bool)));
86 slotChangeZoom(m_doc->zoom());
87 slotSetZone(m_doc->zone());
91 int TrackView::duration() const {
92 return m_trackview->duration();
95 int TrackView::tracksNumber() const {
96 return m_projectTracks - 1;
99 int TrackView::inPoint() const {
100 return m_ruler->inPoint();
103 int TrackView::outPoint() const {
104 return m_ruler->outPoint();
107 void TrackView::slotSetZone(QPoint p) {
111 void TrackView::slotTransitionItemSelected(Transition *t, bool update) {
112 emit transitionItemSelected(t, update);
115 void TrackView::setDuration(int dur) {
116 m_trackview->setDuration(dur);
117 m_ruler->setDuration(dur);
120 void TrackView::parseDocument(QDomDocument doc) {
122 m_documentErrors = QString();
123 // kDebug() << "//// DOCUMENT: " << doc.toString();
124 /*QDomNode props = doc.elementsByTagName("properties").item(0);
125 if (!props.isNull()) {
126 cursorPos = props.toElement().attribute("timeline_position").toInt();
129 // parse project tracks
130 QDomElement tractor = doc.elementsByTagName("tractor").item(0).toElement();
131 QDomNodeList tracks = doc.elementsByTagName("track");
132 QDomNodeList playlists = doc.elementsByTagName("playlist");
134 m_projectTracks = tracks.count();
135 int trackduration = 0;
139 int pos = m_projectTracks - 1;
142 for (int i = 0; i < m_projectTracks; i++) {
143 e = tracks.item(i).toElement();
144 QString playlist_name = e.attribute("producer");
145 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
146 // find playlist related to this track
148 for (int j = 0; j < m_projectTracks; j++) {
149 p = playlists.item(j).toElement();
150 if (p.attribute("id") == playlist_name) break;
152 if (e.attribute("hide") == "video") {
153 m_doc->switchTrackVideo(i - 1, true);
154 } else if (e.attribute("hide") == "audio") {
155 m_doc->switchTrackAudio(i - 1, true);
156 } else if (e.attribute("hide") == "both") {
157 m_doc->switchTrackVideo(i - 1, true);
158 m_doc->switchTrackAudio(i - 1, true);
160 trackduration = slotAddProjectTrack(pos, p);
162 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
163 if (trackduration > duration) duration = trackduration;
165 // background black track
166 for (int j = 0; j < m_projectTracks; j++) {
167 p = playlists.item(j).toElement();
168 if (p.attribute("id") == playlist_name) break;
170 int black_clips = p.childNodes().count();
171 for (int i = 0; i < black_clips; i++)
172 m_doc->loadingProgressed();
173 qApp->processEvents();
179 QDomNodeList transitions = doc.elementsByTagName("transition");
181 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
182 for (int i = 0; i < transitions.count(); i++) {
183 e = transitions.item(i).toElement();
184 QDomNodeList transitionparams = e.childNodes();
185 bool transitionAdd = true;
188 bool isAutomatic = false;
189 bool forceTrack = false;
190 QString mlt_geometry;
192 for (int k = 0; k < transitionparams.count(); k++) {
193 p = transitionparams.item(k).toElement();
195 QString paramName = p.attribute("name");
196 // do not add audio mixing transitions
197 if (paramName == "internal_added" && p.text() == "237") {
198 transitionAdd = false;
199 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
201 } else if (paramName == "a_track") a_track = p.text().toInt();
202 else if (paramName == "b_track") b_track = p.text().toInt();
203 else if (paramName == "mlt_service") mlt_service = p.text();
204 else if (paramName == "geometry") mlt_geometry = p.text();
205 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
206 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
209 if (transitionAdd || mlt_service != "mix") {
210 // Transition should be added to the scene
211 ItemInfo transitionInfo;
212 QString transitionId;
213 if (mlt_service == "composite") {
214 // When adding composite transition, check if it is a wipe transition
215 if (mlt_geometry == "0%,0%:100%x100%") transitionId = "alphatransparency";
216 else if (mlt_geometry.count(';') == 1) {
217 mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
218 mlt_geometry.replace(QChar('x'), QChar(','), Qt::CaseInsensitive);
219 QString start = mlt_geometry.section(';', 0, 0);
220 start = start.section(':', 0, 1);
221 start.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
222 QString end = mlt_geometry.section('=', 1, 1);
223 end = end.section(':', 0, 1);
224 end.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
225 start.append(',' + end);
226 QStringList numbers = start.split(',', QString::SkipEmptyParts);
227 bool isWipeTransition = true;
229 for (int i = 0; i < numbers.size(); ++i) {
230 checkNumber = qAbs(numbers.at(i).toInt());
231 if (checkNumber != 0 && checkNumber != 100) {
232 isWipeTransition = false;
236 if (isWipeTransition) transitionId = "wipe";
239 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
241 for (int k = 0; k < transitionparams.count(); k++) {
242 p = transitionparams.item(k).toElement();
244 QString paramName = p.attribute("name");
245 QString paramValue = p.text();
247 QDomNodeList params = base.elementsByTagName("parameter");
248 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
249 QDomElement e = params.item(i).toElement();
250 if (!e.isNull() && e.attribute("tag") == paramName) {
251 if (e.attribute("type") == "double") {
252 QString factor = e.attribute("factor", "1");
254 double val = paramValue.toDouble() * factor.toDouble();
255 paramValue = QString::number(val);
258 e.setAttribute("value", paramValue);
266 doc.appendChild(doc.importNode(base, true));
267 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
269 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
270 transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
271 transitionInfo.track = m_projectTracks - 1 - b_track;
272 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
273 if (transitionInfo.startPos >= transitionInfo.endPos) {
274 // invalid transition, remove it.
275 m_documentErrors.append(i18n("Removed invalid transition: %1\n", e.attribute("id")));
276 kDebug()<<"///// REMOVED INVALID TRANSITION: "<<e.attribute("id");
277 tractor.removeChild(transitions.item(i));
281 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
282 if (forceTrack) tr->setForcedTrack(true, a_track);
283 m_scene->addItem(tr);
289 QDomNodeList guides = doc.elementsByTagName("guide");
290 for (int i = 0; i < guides.count(); i++) {
291 e = guides.item(i).toElement();
292 const QString comment = e.attribute("comment");
293 const GenTime pos = GenTime(e.attribute("time").toDouble());
294 m_trackview->addGuide(pos, comment);
297 m_trackview->setDuration(duration);
298 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
299 slotRebuildTrackHeaders();
300 if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
301 //m_trackview->setCursorPos(cursorPos);
302 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
305 void TrackView::slotDeleteClip(const QString &clipId) {
306 m_trackview->deleteClip(clipId);
309 void TrackView::setCursorPos(int pos) {
310 m_trackview->setCursorPos(pos);
313 void TrackView::moveCursorPos(int pos) {
314 m_trackview->setCursorPos(pos, false);
317 void TrackView::slotChangeZoom(int factor) {
318 m_doc->setZoom(factor);
319 m_ruler->setPixelPerMark(factor);
320 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
321 m_trackview->setScale(m_scale);
324 int TrackView::fitZoom() const {
325 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
327 for (i = 0; i < 13; i++)
328 if (m_ruler->comboScale[i] > zoom) break;
332 const double TrackView::zoomFactor() const {
336 const int TrackView::mapLocalToValue(int x) const {
337 return (int)(x * zoomFactor());
340 KdenliveDoc *TrackView::document() {
344 void TrackView::refresh() {
345 m_trackview->viewport()->update();
348 void TrackView::slotRebuildTrackHeaders() {
349 QList <TrackInfo> list = m_doc->tracksList();
350 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
351 for (int i = 0; i < widgets.count(); i++)
352 delete widgets.at(i);
353 int max = list.count();
354 for (int i = 0; i < max; i++) {
355 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
356 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
357 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
359 connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
360 connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
361 connect(header, SIGNAL(changeTrack(int)), this, SIGNAL(changeTrack(int)));
362 m_headersLayout->addWidget(header);
364 view->headers_container->adjustSize();
368 int TrackView::slotAddProjectTrack(int ix, QDomElement xml) {
369 int trackTop = KdenliveSettings::trackheight() * ix;
372 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
373 QDomElement elem = n.toElement();
374 if (elem.tagName() == "blank") {
375 position += elem.attribute("length").toInt();
376 } else if (elem.tagName() == "entry") {
377 m_doc->loadingProgressed();
378 qApp->processEvents();
380 int in = elem.attribute("in").toInt();
381 QString idString = elem.attribute("producer");
382 QString id = idString;
383 bool hasSpeedAttribute = false;
385 if (idString.startsWith("slowmotion")) {
386 hasSpeedAttribute = true;
387 id = idString.section(":", 1, 1);
388 speed = idString.section(":", 2, 2).toDouble();
389 } else id = id.section('_', 0, 0);
390 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
392 int out = elem.attribute("out").toInt();
395 clipinfo.startPos = GenTime(position, m_doc->fps());
396 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
397 clipinfo.cropStart = GenTime(in, m_doc->fps());
399 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
400 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), false);
401 if (hasSpeedAttribute) item->setSpeed(speed);
402 m_scene->addItem(item);
403 clip->addReference();
404 position += (out - in + 1);
406 // parse clip effects
407 QDomNodeList effects = elem.childNodes();
408 for (int ix = 0; ix < effects.count(); ix++) {
409 QDomElement effect = effects.at(ix).toElement();
410 if (effect.tagName() == "filter") {
411 // add effect to clip
415 QString ladspaEffectFile;
416 // Get effect tag & index
417 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
418 // parse effect parameters
419 QDomElement effectparam = n3.toElement();
420 if (effectparam.attribute("name") == "tag") {
421 effecttag = effectparam.text();
422 } else if (effectparam.attribute("name") == "kdenlive_id") {
423 effectid = effectparam.text();
424 } else if (effectparam.attribute("name") == "kdenlive_ix") {
425 effectindex = effectparam.text();
426 } else if (effectparam.attribute("name") == "src") {
427 ladspaEffectFile = effectparam.text();
428 if (!QFile::exists(ladspaEffectFile)) {
429 // If the ladspa effect file is missing, recreate it
430 kDebug() << "// MISSING LADSPA FILE: " << ladspaEffectFile;
431 ladspaEffectFile = m_doc->getLadspaFile();
432 effectparam.firstChild().setNodeValue(ladspaEffectFile);
433 kDebug() << "// ... REPLACED WITH: " << ladspaEffectFile;
437 //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
438 // get effect standard tags
439 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
440 if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
441 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
442 if (clipeffect.isNull()) {
443 kDebug() << "/// WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
444 m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
445 elem.removeChild(effects.at(ix));
448 QDomElement currenteffect = clipeffect.cloneNode().toElement();
449 currenteffect.setAttribute("kdenlive_ix", effectindex);
450 QDomNodeList clipeffectparams = currenteffect.childNodes();
452 if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
453 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
454 // effect is key-framable, read all effects to retrieve keyframes
458 QDomNodeList params = currenteffect.elementsByTagName("parameter");
459 for (int i = 0; i < params.count(); i++) {
460 QDomElement e = params.item(i).toElement();
461 if (e.attribute("type") == "keyframe") {
462 starttag = e.attribute("starttag", "start");
463 endtag = e.attribute("endtag", "end");
464 factor = e.attribute("factor", "1").toDouble();
469 int effectin = effect.attribute("in").toInt();
470 int effectout = effect.attribute("out").toInt();
473 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
474 // parse effect parameters
475 QDomElement effectparam = n3.toElement();
476 if (effectparam.attribute("name") == starttag)
477 startvalue = effectparam.text().toDouble() * factor;
478 if (effectparam.attribute("name") == endtag)
479 endvalue = effectparam.text().toDouble() * factor;
481 // add first keyframe
482 keyframes.append(QString::number(effectin) + ":" + QString::number(startvalue) + ";" + QString::number(effectout) + ":" + QString::number(endvalue) + ";");
483 QDomNode lastParsedEffect;
485 QDomNode n2 = effects.at(ix);
486 bool continueParsing = true;
487 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
489 QDomElement kfreffect = n2.toElement();
490 int effectout = kfreffect.attribute("out").toInt();
492 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
493 // parse effect parameters
494 QDomElement subeffectparam = n4.toElement();
495 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
496 //We are not in the same effect, stop parsing
497 lastParsedEffect = n2.previousSibling();
499 continueParsing = false;
501 } else if (subeffectparam.attribute("name") == endtag) {
502 endvalue = subeffectparam.text().toDouble() * factor;
506 if (continueParsing) {
507 keyframes.append(QString::number(effectout) + ":" + QString::number(endvalue) + ";");
512 params = currenteffect.elementsByTagName("parameter");
513 for (int i = 0; i < params.count(); i++) {
514 QDomElement e = params.item(i).toElement();
515 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
517 if (!continueParsing) {
518 n2 = lastParsedEffect;
521 // Check if effect has in/out points
522 if (effect.hasAttribute("in")) {
523 EffectsList::setParameter(currenteffect, "in", effect.attribute("in"));
525 if (effect.hasAttribute("out")) {
526 EffectsList::setParameter(currenteffect, "out", effect.attribute("out"));
530 // adjust effect parameters
531 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
532 // parse effect parameters
533 QDomElement effectparam = n3.toElement();
534 QString paramname = effectparam.attribute("name");
535 QString paramvalue = effectparam.text();
538 // try to find this parameter in the effect xml
540 for (int k = 0; k < clipeffectparams.count(); k++) {
541 e = clipeffectparams.item(k).toElement();
542 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
543 double factor = e.attribute("factor", "1").toDouble();
545 e.setAttribute("value", paramvalue.toDouble() * factor);
546 } else e.setAttribute("value", paramvalue);
551 if (effecttag == "ladspa") {
552 //QString ladspaEffectFile = EffectsList::parameter(effect, "src", "property");
554 if (!QFile::exists(ladspaEffectFile)) {
555 // If the ladspa effect file is missing, recreate it
556 initEffects::ladspaEffectFile(ladspaEffectFile, currenteffect.attribute("ladspaid").toInt(), m_trackview->getLadspaParams(currenteffect));
558 currenteffect.setAttribute("src", ladspaEffectFile);
560 item->addEffect(currenteffect, false);
561 item->effectsCounter();
565 } else kWarning() << "CANNOT INSERT CLIP " << id;
566 //m_clipList.append(clip);
569 //m_trackDuration = position;
572 //documentTracks.insert(ix, track);
573 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
578 QGraphicsScene *TrackView::projectScene() {
582 CustomTrackView *TrackView::projectView() {
586 void TrackView::setEditMode(const QString & editMode) {
587 m_editMode = editMode;
590 const QString & TrackView::editMode() const {
596 #include "trackview.moc"