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());
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 QDomNodeList tracks = doc.elementsByTagName("track");
131 QDomNodeList playlists = doc.elementsByTagName("playlist");
133 m_projectTracks = tracks.count();
134 int trackduration = 0;
138 int pos = m_projectTracks - 1;
141 for (int i = 0; i < m_projectTracks; i++) {
142 e = tracks.item(i).toElement();
143 QString playlist_name = e.attribute("producer");
144 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
145 // find playlist related to this track
147 for (int j = 0; j < m_projectTracks; j++) {
148 p = playlists.item(j).toElement();
149 if (p.attribute("id") == playlist_name) break;
151 if (e.attribute("hide") == "video") {
152 m_doc->switchTrackVideo(i - 1, true);
153 } else if (e.attribute("hide") == "audio") {
154 m_doc->switchTrackAudio(i - 1, true);
155 } else if (e.attribute("hide") == "both") {
156 m_doc->switchTrackVideo(i - 1, true);
157 m_doc->switchTrackAudio(i - 1, true);
159 trackduration = slotAddProjectTrack(pos, p);
161 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
162 if (trackduration > duration) duration = trackduration;
164 // background black track
165 for (int j = 0; j < m_projectTracks; j++) {
166 p = playlists.item(j).toElement();
167 if (p.attribute("id") == playlist_name) break;
169 int black_clips = p.childNodes().count();
170 for (int i = 0; i < black_clips; i++)
171 m_doc->loadingProgressed();
172 qApp->processEvents();
178 QDomNodeList transitions = doc.elementsByTagName("transition");
179 int projectTransitions = transitions.count();
180 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
181 for (int i = 0; i < projectTransitions; i++) {
182 e = transitions.item(i).toElement();
183 QDomNodeList transitionparams = e.childNodes();
184 bool transitionAdd = true;
187 bool isAutomatic = false;
188 bool forceTrack = false;
189 QString mlt_geometry;
191 for (int k = 0; k < transitionparams.count(); k++) {
192 p = transitionparams.item(k).toElement();
194 QString paramName = p.attribute("name");
195 // do not add audio mixing transitions
196 if (paramName == "internal_added" && p.text() == "237") {
197 transitionAdd = false;
198 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
200 } else if (paramName == "a_track") a_track = p.text().toInt();
201 else if (paramName == "b_track") b_track = p.text().toInt();
202 else if (paramName == "mlt_service") mlt_service = p.text();
203 else if (paramName == "geometry") mlt_geometry = p.text();
204 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
205 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
208 if (transitionAdd || mlt_service != "mix") {
209 // Transition should be added to the scene
210 ItemInfo transitionInfo;
211 QString transitionId;
212 if (mlt_service == "composite") {
213 // When adding composite transition, check if it is a wipe transition
214 if (mlt_geometry == "0%,0%:100%x100%") transitionId = "alphatransparency";
215 else if (mlt_geometry.count(';') == 1) {
216 mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
217 mlt_geometry.replace(QChar('x'), QChar(','), Qt::CaseInsensitive);
218 QString start = mlt_geometry.section(';', 0, 0);
219 start = start.section(':', 0, 1);
220 start.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
221 QString end = mlt_geometry.section('=', 1, 1);
222 end = end.section(':', 0, 1);
223 end.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
224 start.append(',' + end);
225 QStringList numbers = start.split(',', QString::SkipEmptyParts);
226 bool isWipeTransition = true;
228 for (int i = 0; i < numbers.size(); ++i) {
229 checkNumber = qAbs(numbers.at(i).toInt());
230 if (checkNumber != 0 && checkNumber != 100) {
231 isWipeTransition = false;
235 if (isWipeTransition) transitionId = "wipe";
238 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
240 for (int k = 0; k < transitionparams.count(); k++) {
241 p = transitionparams.item(k).toElement();
243 QString paramName = p.attribute("name");
244 QString paramValue = p.text();
246 QDomNodeList params = base.elementsByTagName("parameter");
247 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
248 QDomElement e = params.item(i).toElement();
249 if (!e.isNull() && e.attribute("tag") == paramName) {
250 if (e.attribute("type") == "double") {
251 QString factor = e.attribute("factor", "1");
253 double val = paramValue.toDouble() * factor.toDouble();
254 paramValue = QString::number(val);
257 e.setAttribute("value", paramValue);
265 doc.appendChild(doc.importNode(base, true));
266 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
268 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
269 transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
270 transitionInfo.track = m_projectTracks - 1 - b_track;
271 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
272 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
273 if (forceTrack) tr->setForcedTrack(true, a_track);
274 m_scene->addItem(tr);
279 QDomNodeList guides = doc.elementsByTagName("guide");
280 for (int i = 0; i < guides.count(); i++) {
281 e = guides.item(i).toElement();
282 const QString comment = e.attribute("comment");
283 const GenTime pos = GenTime(e.attribute("time").toDouble());
284 m_trackview->addGuide(pos, comment);
287 m_trackview->setDuration(duration);
288 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
289 slotRebuildTrackHeaders();
290 if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
291 //m_trackview->setCursorPos(cursorPos);
292 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
295 void TrackView::slotDeleteClip(const QString &clipId) {
296 m_trackview->deleteClip(clipId);
299 void TrackView::setCursorPos(int pos) {
300 m_trackview->setCursorPos(pos);
303 void TrackView::moveCursorPos(int pos) {
304 m_trackview->setCursorPos(pos, false);
307 void TrackView::slotChangeZoom(int factor) {
308 m_doc->setZoom(factor);
309 m_ruler->setPixelPerMark(factor);
310 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
311 m_trackview->setScale(m_scale);
314 int TrackView::fitZoom() const {
315 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
317 for (i = 0; i < 13; i++)
318 if (m_ruler->comboScale[i] > zoom) break;
322 const double TrackView::zoomFactor() const {
326 const int TrackView::mapLocalToValue(int x) const {
327 return (int)(x * zoomFactor());
330 KdenliveDoc *TrackView::document() {
334 void TrackView::refresh() {
335 m_trackview->viewport()->update();
338 void TrackView::slotRebuildTrackHeaders() {
339 QList <TrackInfo> list = m_doc->tracksList();
340 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
341 for (int i = 0; i < widgets.count(); i++)
342 delete widgets.at(i);
343 int max = list.count();
344 for (int i = 0; i < max; i++) {
345 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
346 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
347 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
349 connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
350 connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
351 connect(header, SIGNAL(changeTrack(int)), this, SIGNAL(changeTrack(int)));
352 m_headersLayout->addWidget(header);
354 view->headers_container->adjustSize();
358 int TrackView::slotAddProjectTrack(int ix, QDomElement xml) {
359 int trackTop = KdenliveSettings::trackheight() * ix;
362 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
363 QDomElement elem = n.toElement();
364 if (elem.tagName() == "blank") {
365 position += elem.attribute("length").toInt();
366 } else if (elem.tagName() == "entry") {
367 m_doc->loadingProgressed();
368 qApp->processEvents();
370 int in = elem.attribute("in").toInt();
371 QString idString = elem.attribute("producer");
372 QString id = idString;
373 bool hasSpeedAttribute = false;
375 if (idString.startsWith("slowmotion")) {
376 hasSpeedAttribute = true;
377 id = idString.section(":", 1, 1);
378 speed = idString.section(":", 2, 2).toDouble();
379 } else id = id.section('_', 0, 0);
380 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
382 int out = elem.attribute("out").toInt();
385 clipinfo.startPos = GenTime(position, m_doc->fps());
386 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
387 clipinfo.cropStart = GenTime(in, m_doc->fps());
389 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
390 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), false);
391 if (hasSpeedAttribute) item->setSpeed(speed);
392 m_scene->addItem(item);
393 clip->addReference();
394 position += (out - in + 1);
396 // parse clip effects
397 QDomNodeList effects = elem.childNodes();
398 for (int ix = 0; ix < effects.count(); ix++) {
399 QDomElement effect = effects.at(ix).toElement();
400 if (effect.tagName() == "filter") {
401 // add effect to clip
405 QString ladspaEffectFile;
406 // Get effect tag & index
407 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
408 // parse effect parameters
409 QDomElement effectparam = n3.toElement();
410 if (effectparam.attribute("name") == "tag") {
411 effecttag = effectparam.text();
412 } else if (effectparam.attribute("name") == "kdenlive_id") {
413 effectid = effectparam.text();
414 } else if (effectparam.attribute("name") == "kdenlive_ix") {
415 effectindex = effectparam.text();
416 } else if (effectparam.attribute("name") == "src") {
417 ladspaEffectFile = effectparam.text();
418 if (!QFile::exists(ladspaEffectFile)) {
419 // If the ladspa effect file is missing, recreate it
420 kDebug() << "// MISSING LADSPA FILE: " << ladspaEffectFile;
421 ladspaEffectFile = m_doc->getLadspaFile();
422 effectparam.firstChild().setNodeValue(ladspaEffectFile);
423 kDebug() << "// ... REPLACED WITH: " << ladspaEffectFile;
427 //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
428 // get effect standard tags
429 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
430 if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
431 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
432 if (clipeffect.isNull()) {
433 kDebug() << "/// WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
434 m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
435 elem.removeChild(effects.at(ix));
438 QDomElement currenteffect = clipeffect.cloneNode().toElement();
439 currenteffect.setAttribute("kdenlive_ix", effectindex);
440 QDomNodeList clipeffectparams = currenteffect.childNodes();
442 if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
443 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
444 // effect is key-framable, read all effects to retrieve keyframes
448 QDomNodeList params = currenteffect.elementsByTagName("parameter");
449 for (int i = 0; i < params.count(); i++) {
450 QDomElement e = params.item(i).toElement();
451 if (e.attribute("type") == "keyframe") {
452 starttag = e.attribute("starttag", "start");
453 endtag = e.attribute("endtag", "end");
454 factor = e.attribute("factor", "1").toDouble();
459 int effectin = effect.attribute("in").toInt();
460 int effectout = effect.attribute("out").toInt();
463 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
464 // parse effect parameters
465 QDomElement effectparam = n3.toElement();
466 if (effectparam.attribute("name") == starttag)
467 startvalue = effectparam.text().toDouble() * factor;
468 if (effectparam.attribute("name") == endtag)
469 endvalue = effectparam.text().toDouble() * factor;
471 // add first keyframe
472 keyframes.append(QString::number(effectin) + ":" + QString::number(startvalue) + ";" + QString::number(effectout) + ":" + QString::number(endvalue) + ";");
473 QDomNode lastParsedEffect;
475 QDomNode n2 = effects.at(ix);
476 bool continueParsing = true;
477 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
479 QDomElement kfreffect = n2.toElement();
480 int effectout = kfreffect.attribute("out").toInt();
482 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
483 // parse effect parameters
484 QDomElement subeffectparam = n4.toElement();
485 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
486 //We are not in the same effect, stop parsing
487 lastParsedEffect = n2.previousSibling();
489 continueParsing = false;
491 } else if (subeffectparam.attribute("name") == endtag) {
492 endvalue = subeffectparam.text().toDouble() * factor;
496 if (continueParsing) {
497 keyframes.append(QString::number(effectout) + ":" + QString::number(endvalue) + ";");
502 params = currenteffect.elementsByTagName("parameter");
503 for (int i = 0; i < params.count(); i++) {
504 QDomElement e = params.item(i).toElement();
505 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
507 if (!continueParsing) {
508 n2 = lastParsedEffect;
511 // Check if effect has in/out points
512 if (effect.hasAttribute("in")) {
513 EffectsList::setParameter(currenteffect, "in", effect.attribute("in"));
515 if (effect.hasAttribute("out")) {
516 EffectsList::setParameter(currenteffect, "out", effect.attribute("out"));
520 // adjust effect parameters
521 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
522 // parse effect parameters
523 QDomElement effectparam = n3.toElement();
524 QString paramname = effectparam.attribute("name");
525 QString paramvalue = effectparam.text();
528 // try to find this parameter in the effect xml
530 for (int k = 0; k < clipeffectparams.count(); k++) {
531 e = clipeffectparams.item(k).toElement();
532 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
533 double factor = e.attribute("factor", "1").toDouble();
535 e.setAttribute("value", paramvalue.toDouble() * factor);
536 } else e.setAttribute("value", paramvalue);
541 if (effecttag == "ladspa") {
542 //QString ladspaEffectFile = EffectsList::parameter(effect, "src", "property");
544 if (!QFile::exists(ladspaEffectFile)) {
545 // If the ladspa effect file is missing, recreate it
546 initEffects::ladspaEffectFile(ladspaEffectFile, currenteffect.attribute("ladspaid").toInt(), m_trackview->getLadspaParams(currenteffect));
548 currenteffect.setAttribute("src", ladspaEffectFile);
550 item->addEffect(currenteffect, false);
551 item->effectsCounter();
555 } else kWarning() << "CANNOT INSERT CLIP " << id;
556 //m_clipList.append(clip);
559 //m_trackDuration = position;
562 //documentTracks.insert(ix, track);
563 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
568 QGraphicsScene *TrackView::projectScene() {
572 CustomTrackView *TrackView::projectView() {
576 void TrackView::setEditMode(const QString & editMode) {
577 m_editMode = editMode;
580 const QString & TrackView::editMode() const {
586 #include "trackview.moc"