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"
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());
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());
90 int TrackView::duration() const {
91 return m_trackview->duration();
94 int TrackView::tracksNumber() const {
95 return m_projectTracks - 1;
98 int TrackView::inPoint() const {
99 return m_ruler->inPoint();
102 int TrackView::outPoint() const {
103 return m_ruler->outPoint();
106 void TrackView::slotSetZone(QPoint p) {
110 void TrackView::slotTransitionItemSelected(Transition *t, bool update) {
111 emit transitionItemSelected(t, update);
114 void TrackView::setDuration(int dur) {
115 m_trackview->setDuration(dur);
116 m_ruler->setDuration(dur);
119 void TrackView::parseDocument(QDomDocument doc) {
121 m_documentErrors = QString();
122 // kDebug() << "//// DOCUMENT: " << doc.toString();
123 /*QDomNode props = doc.elementsByTagName("properties").item(0);
124 if (!props.isNull()) {
125 cursorPos = props.toElement().attribute("timeline_position").toInt();
128 // parse project tracks
129 QDomNodeList tracks = doc.elementsByTagName("track");
130 QDomNodeList playlists = doc.elementsByTagName("playlist");
132 m_projectTracks = tracks.count();
133 int trackduration = 0;
137 int pos = m_projectTracks - 1;
140 for (int i = 0; i < m_projectTracks; i++) {
141 e = tracks.item(i).toElement();
142 QString playlist_name = e.attribute("producer");
143 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
144 // find playlist related to this track
146 for (int j = 0; j < m_projectTracks; j++) {
147 p = playlists.item(j).toElement();
148 if (p.attribute("id") == playlist_name) break;
150 if (e.attribute("hide") == "video") {
151 m_doc->switchTrackVideo(i - 1, true);
152 } else if (e.attribute("hide") == "audio") {
153 m_doc->switchTrackAudio(i - 1, true);
154 } else if (e.attribute("hide") == "both") {
155 m_doc->switchTrackVideo(i - 1, true);
156 m_doc->switchTrackAudio(i - 1, true);
158 trackduration = slotAddProjectTrack(pos, p);
160 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
161 if (trackduration > duration) duration = trackduration;
163 // background black track
164 for (int j = 0; j < m_projectTracks; j++) {
165 p = playlists.item(j).toElement();
166 if (p.attribute("id") == playlist_name) break;
168 int black_clips = p.childNodes().count();
169 for (int i = 0; i < black_clips; i++)
170 m_doc->loadingProgressed();
171 qApp->processEvents();
177 QDomNodeList transitions = doc.elementsByTagName("transition");
178 int projectTransitions = transitions.count();
179 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
180 for (int i = 0; i < projectTransitions; i++) {
181 e = transitions.item(i).toElement();
182 QDomNodeList transitionparams = e.childNodes();
183 bool transitionAdd = true;
186 bool isAutomatic = false;
187 bool forceTrack = false;
188 QString mlt_geometry;
190 for (int k = 0; k < transitionparams.count(); k++) {
191 p = transitionparams.item(k).toElement();
193 QString paramName = p.attribute("name");
194 // do not add audio mixing transitions
195 if (paramName == "internal_added" && p.text() == "237") {
196 transitionAdd = false;
197 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
199 } else if (paramName == "a_track") a_track = p.text().toInt();
200 else if (paramName == "b_track") b_track = p.text().toInt();
201 else if (paramName == "mlt_service") mlt_service = p.text();
202 else if (paramName == "geometry") mlt_geometry = p.text();
203 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
204 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
207 if (transitionAdd || mlt_service != "mix") {
208 // Transition should be added to the scene
209 ItemInfo transitionInfo;
210 QString transitionId;
211 if (mlt_service == "composite") {
212 // When adding composite transition, check if it is a wipe transition
213 if (mlt_geometry == "0%,0%:100%x100%") transitionId = "alphatransparency";
214 else if (mlt_geometry.count(';') == 1) {
215 mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
216 mlt_geometry.replace(QChar('x'), QChar(','), Qt::CaseInsensitive);
217 QString start = mlt_geometry.section(';', 0, 0);
218 start = start.section(':', 0, 1);
219 start.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
220 QString end = mlt_geometry.section('=', 1, 1);
221 end = end.section(':', 0, 1);
222 end.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
223 start.append(',' + end);
224 QStringList numbers = start.split(',', QString::SkipEmptyParts);
225 bool isWipeTransition = true;
227 for (int i = 0; i < numbers.size(); ++i) {
228 checkNumber = qAbs(numbers.at(i).toInt());
229 if (checkNumber != 0 && checkNumber != 100) {
230 isWipeTransition = false;
234 if (isWipeTransition) transitionId = "wipe";
237 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
239 for (int k = 0; k < transitionparams.count(); k++) {
240 p = transitionparams.item(k).toElement();
242 QString paramName = p.attribute("name");
243 QString paramValue = p.text();
245 QDomNodeList params = base.elementsByTagName("parameter");
246 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
247 QDomElement e = params.item(i).toElement();
248 if (!e.isNull() && e.attribute("tag") == paramName) {
249 if (e.attribute("type") == "double") {
250 QString factor = e.attribute("factor", "1");
252 double val = paramValue.toDouble() * factor.toDouble();
253 paramValue = QString::number(val);
256 e.setAttribute("value", paramValue);
264 doc.appendChild(doc.importNode(base, true));
265 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
267 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
268 transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
269 transitionInfo.track = m_projectTracks - 1 - b_track;
270 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
271 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
272 if (forceTrack) tr->setForcedTrack(true, a_track);
273 m_scene->addItem(tr);
278 QDomNodeList guides = doc.elementsByTagName("guide");
279 for (int i = 0; i < guides.count(); i++) {
280 e = guides.item(i).toElement();
281 const QString comment = e.attribute("comment");
282 const GenTime pos = GenTime(e.attribute("time").toDouble());
283 m_trackview->addGuide(pos, comment);
286 m_trackview->setDuration(duration);
287 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
288 slotRebuildTrackHeaders();
289 if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
290 //m_trackview->setCursorPos(cursorPos);
291 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
294 void TrackView::slotDeleteClip(const QString &clipId) {
295 m_trackview->deleteClip(clipId);
298 void TrackView::setCursorPos(int pos) {
299 m_trackview->setCursorPos(pos);
302 void TrackView::moveCursorPos(int pos) {
303 m_trackview->setCursorPos(pos, false);
306 void TrackView::slotChangeZoom(int factor) {
307 m_doc->setZoom(factor);
308 m_ruler->setPixelPerMark(factor);
309 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
310 m_trackview->setScale(m_scale);
313 int TrackView::fitZoom() const {
314 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
316 for (i = 0; i < 13; i++)
317 if (m_ruler->comboScale[i] > zoom) break;
321 const double TrackView::zoomFactor() const {
325 const int TrackView::mapLocalToValue(int x) const {
326 return (int)(x * zoomFactor());
329 KdenliveDoc *TrackView::document() {
333 void TrackView::refresh() {
334 m_trackview->viewport()->update();
337 void TrackView::slotRebuildTrackHeaders() {
338 QList <TrackInfo> list = m_doc->tracksList();
339 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
340 for (int i = 0; i < widgets.count(); i++)
341 delete widgets.at(i);
342 int max = list.count();
343 for (int i = 0; i < max; i++) {
344 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
345 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
346 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
348 connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
349 connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
350 connect(header, SIGNAL(changeTrack(int)), this, SIGNAL(changeTrack(int)));
351 m_headersLayout->addWidget(header);
353 view->headers_container->adjustSize();
357 int TrackView::slotAddProjectTrack(int ix, QDomElement xml) {
358 int trackTop = KdenliveSettings::trackheight() * ix;
361 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
362 QDomElement elem = n.toElement();
363 if (elem.tagName() == "blank") {
364 position += elem.attribute("length").toInt();
365 } else if (elem.tagName() == "entry") {
366 m_doc->loadingProgressed();
367 qApp->processEvents();
369 int in = elem.attribute("in").toInt();
370 QString idString = elem.attribute("producer");
371 QString id = idString;
372 bool hasSpeedAttribute = false;
374 if (idString.startsWith("slowmotion")) {
375 hasSpeedAttribute = true;
376 id = idString.section(":", 1, 1);
377 speed = idString.section(":", 2, 2).toDouble();
378 } else id = id.section('_', 0, 0);
379 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
381 int out = elem.attribute("out").toInt();
384 clipinfo.startPos = GenTime(position, m_doc->fps());
385 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
386 clipinfo.cropStart = GenTime(in, m_doc->fps());
388 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
389 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), false);
390 if (hasSpeedAttribute) item->setSpeed(speed);
391 m_scene->addItem(item);
392 clip->addReference();
393 position += (out - in + 1);
395 // parse clip effects
396 QDomNodeList effects = elem.childNodes();
397 for (int ix = 0; ix < effects.count(); ix++) {
398 QDomElement effect = effects.at(ix).toElement();
399 if (effect.tagName() == "filter") {
400 // add effect to clip
404 // Get effect tag & index
405 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
406 // parse effect parameters
407 QDomElement effectparam = n3.toElement();
408 if (effectparam.attribute("name") == "tag") {
409 effecttag = effectparam.text();
410 } else if (effectparam.attribute("name") == "kdenlive_id") {
411 effectid = effectparam.text();
412 } else if (effectparam.attribute("name") == "kdenlive_ix") {
413 effectindex = effectparam.text();
416 //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
417 // get effect standard tags
418 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
419 if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
420 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
421 if (clipeffect.isNull()) {
422 kDebug() << "/// WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
423 m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
424 elem.removeChild(effects.at(ix));
427 QDomElement currenteffect = clipeffect.cloneNode().toElement();
428 currenteffect.setAttribute("kdenlive_ix", effectindex);
429 QDomNodeList clipeffectparams = currenteffect.childNodes();
431 if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
432 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
433 // effect is key-framable, read all effects to retrieve keyframes
437 QDomNodeList params = currenteffect.elementsByTagName("parameter");
438 for (int i = 0; i < params.count(); i++) {
439 QDomElement e = params.item(i).toElement();
440 if (e.attribute("type") == "keyframe") {
441 starttag = e.attribute("starttag", "start");
442 endtag = e.attribute("endtag", "end");
443 factor = e.attribute("factor", "1").toDouble();
448 int effectin = effect.attribute("in").toInt();
449 int effectout = effect.attribute("out").toInt();
452 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
453 // parse effect parameters
454 QDomElement effectparam = n3.toElement();
455 if (effectparam.attribute("name") == starttag)
456 startvalue = effectparam.text().toDouble() * factor;
457 if (effectparam.attribute("name") == endtag)
458 endvalue = effectparam.text().toDouble() * factor;
460 // add first keyframe
461 keyframes.append(QString::number(effectin) + ":" + QString::number(startvalue) + ";" + QString::number(effectout) + ":" + QString::number(endvalue) + ";");
462 QDomNode lastParsedEffect;
464 QDomNode n2 = effects.at(ix);
465 bool continueParsing = true;
466 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
468 QDomElement kfreffect = n2.toElement();
469 int effectout = kfreffect.attribute("out").toInt();
471 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
472 // parse effect parameters
473 QDomElement subeffectparam = n4.toElement();
474 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
475 //We are not in the same effect, stop parsing
476 lastParsedEffect = n2.previousSibling();
478 continueParsing = false;
480 } else if (subeffectparam.attribute("name") == endtag) {
481 endvalue = subeffectparam.text().toDouble() * factor;
485 if (continueParsing) {
486 keyframes.append(QString::number(effectout) + ":" + QString::number(endvalue) + ";");
491 params = currenteffect.elementsByTagName("parameter");
492 for (int i = 0; i < params.count(); i++) {
493 QDomElement e = params.item(i).toElement();
494 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
496 if (!continueParsing) {
497 n2 = lastParsedEffect;
500 // Check if effect has in/out points
501 if (effect.hasAttribute("in")) {
502 EffectsList::setParameter(currenteffect, "in", effect.attribute("in"));
504 if (effect.hasAttribute("out")) {
505 EffectsList::setParameter(currenteffect, "out", effect.attribute("out"));
509 // adjust effect parameters
510 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
511 // parse effect parameters
512 QDomElement effectparam = n3.toElement();
513 QString paramname = effectparam.attribute("name");
514 QString paramvalue = effectparam.text();
516 // try to find this parameter in the effect xml
518 for (int k = 0; k < clipeffectparams.count(); k++) {
519 e = clipeffectparams.item(k).toElement();
520 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
521 e.setAttribute("value", paramvalue);
526 item->addEffect(currenteffect, false);
527 item->effectsCounter();
531 } else kWarning() << "CANNOT INSERT CLIP " << id;
532 //m_clipList.append(clip);
535 //m_trackDuration = position;
538 //documentTracks.insert(ix, track);
539 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
544 QGraphicsScene *TrackView::projectScene() {
548 CustomTrackView *TrackView::projectView() {
552 void TrackView::setEditMode(const QString & editMode) {
553 m_editMode = editMode;
556 const QString & TrackView::editMode() const {
562 #include "trackview.moc"