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 ***************************************************************************/
21 #include "trackview.h"
22 #include "headertrack.h"
24 #include "transition.h"
25 #include "kdenlivesettings.h"
26 #include "clipmanager.h"
27 #include "customruler.h"
28 #include "kdenlivedoc.h"
29 #include "mainwindow.h"
30 #include "customtrackview.h"
31 #include "initeffects.h"
32 #include "profilesdialog.h"
33 #include "configtrackscommand.h"
36 #include <KMessageBox>
37 #include <KIO/NetAccess>
40 #include <QInputDialog>
42 TrackView::TrackView(KdenliveDoc *doc, bool *ok, QWidget *parent) :
51 // ruler_frame->setMaximumHeight();
52 // size_frame->setMaximumHeight();
53 m_scene = new CustomTrackScene(doc);
54 m_trackview = new CustomTrackView(doc, m_scene, parent);
55 m_trackview->scale(1, 1);
56 m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
58 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
59 connect(m_ruler, SIGNAL(zoneMoved(int, int)), this, SIGNAL(zoneMoved(int, int)));
60 connect(m_ruler, SIGNAL(adjustZoom(int)), this, SIGNAL(setZoom(int)));
61 QHBoxLayout *layout = new QHBoxLayout;
62 layout->setContentsMargins(m_trackview->frameWidth(), 0, 0, 0);
63 layout->setSpacing(0);
64 ruler_frame->setLayout(layout);
65 layout->addWidget(m_ruler);
67 QHBoxLayout *sizeLayout = new QHBoxLayout;
68 sizeLayout->setContentsMargins(0, 0, 0, 0);
69 sizeLayout->setSpacing(0);
70 size_frame->setLayout(sizeLayout);
72 QToolButton *butSmall = new QToolButton(this);
73 butSmall->setIcon(KIcon("kdenlive-zoom-small"));
74 butSmall->setToolTip(i18n("Smaller tracks"));
75 butSmall->setAutoRaise(true);
76 connect(butSmall, SIGNAL(clicked()), this, SLOT(slotVerticalZoomDown()));
77 sizeLayout->addWidget(butSmall);
79 QToolButton *butLarge = new QToolButton(this);
80 butLarge->setIcon(KIcon("kdenlive-zoom-large"));
81 butLarge->setToolTip(i18n("Bigger tracks"));
82 butLarge->setAutoRaise(true);
83 connect(butLarge, SIGNAL(clicked()), this, SLOT(slotVerticalZoomUp()));
84 sizeLayout->addWidget(butLarge);
86 QHBoxLayout *tracksLayout = new QHBoxLayout;
87 tracksLayout->setContentsMargins(0, 0, 0, 0);
88 tracksLayout->setSpacing(0);
89 tracks_frame->setLayout(tracksLayout);
91 headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
92 headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
93 headers_area->setFixedWidth(70);
95 QVBoxLayout *headersLayout = new QVBoxLayout;
96 headersLayout->setContentsMargins(0, m_trackview->frameWidth(), 0, 0);
97 headersLayout->setSpacing(0);
98 headers_container->setLayout(headersLayout);
99 connect(headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
101 tracksLayout->addWidget(m_trackview);
102 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), headers_area->verticalScrollBar(), SLOT(setValue(int)));
103 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
104 connect(m_trackview, SIGNAL(tracksChanged()), this, SLOT(slotReloadTracks()));
105 connect(m_trackview, SIGNAL(updateTrackHeaders()), this, SLOT(slotRepaintTracks()));
106 connect(m_trackview, SIGNAL(showTrackEffects(int, TrackInfo)), this, SIGNAL(showTrackEffects(int, TrackInfo)));
107 connect(m_trackview, SIGNAL(updateTrackEffectState(int)), this, SLOT(slotUpdateTrackEffectState(int)));
110 parseDocument(m_doc->toXml());
111 if (m_doc->setSceneList() == -1) *ok = false;
113 connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
114 connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
115 connect(m_trackview->horizontalScrollBar(), SIGNAL(rangeChanged(int, int)), this, SLOT(slotUpdateVerticalScroll(int, int)));
116 connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
117 connect(m_trackview, SIGNAL(doTrackLock(int, bool)), this, SLOT(slotChangeTrackLock(int, bool)));
119 slotChangeZoom(m_doc->zoom().x(), m_doc->zoom().y());
120 slotSetZone(m_doc->zone(), false);
123 TrackView::~TrackView()
130 void TrackView::keyPressEvent(QKeyEvent * event)
132 if (event->key() == Qt::Key_Up) {
133 m_trackview->slotTrackUp();
135 } else if (event->key() == Qt::Key_Down) {
136 m_trackview->slotTrackDown();
138 } else QWidget::keyPressEvent(event);
141 int TrackView::duration() const
143 return m_trackview->duration();
146 int TrackView::tracksNumber() const
148 return m_projectTracks - 1;
151 bool TrackView::checkProjectAudio() const
153 bool hasAudio = false;
154 const QList <TrackInfo> list = m_doc->tracksList();
155 int max = list.count();
156 for (int i = 0; i < max; i++) {
157 TrackInfo info = list.at(max - i - 1);
158 if (!info.isMute && m_trackview->hasAudio(i)) {
166 int TrackView::inPoint() const
168 return m_ruler->inPoint();
171 int TrackView::outPoint() const
173 return m_ruler->outPoint();
176 void TrackView::slotSetZone(QPoint p, bool updateDocumentProperties)
179 if (updateDocumentProperties) m_doc->setZone(p.x(), p.y());
182 void TrackView::setDuration(int dur)
184 m_trackview->setDuration(dur);
185 m_ruler->setDuration(dur);
188 void TrackView::parseDocument(QDomDocument doc)
191 m_documentErrors.clear();
193 //kDebug() << "//// DOCUMENT: " << doc.toString();
194 /*QDomNode props = doc.elementsByTagName("properties").item(0);
195 if (!props.isNull()) {
196 cursorPos = props.toElement().attribute("timeline_position").toInt();
199 // parse project tracks
200 QDomElement mlt = doc.firstChildElement("mlt");
201 QDomElement tractor = mlt.firstChildElement("tractor");
202 QDomNodeList tracks = tractor.elementsByTagName("track");
203 QDomNodeList playlists = doc.elementsByTagName("playlist");
205 m_projectTracks = tracks.count();
206 int trackduration = 0;
210 int pos = m_projectTracks - 1;
211 m_invalidProducers.clear();
212 QDomNodeList producers = doc.elementsByTagName("producer");
213 for (int i = 0; i < producers.count(); i++) {
214 // Check for invalid producers
215 QDomNode n = producers.item(i);
219 // Check for invalid markup
220 QDomNodeList params = e.elementsByTagName("property");
221 for (int j = 0; j < params.count(); j++) {
222 QDomElement p = params.item(j).toElement();
223 if (p.attribute("name") == "markup") {
224 QString val = p.text().toUtf8().data();
225 kDebug()<<"//FOUND MARKUP, VAL: "<<val;
226 //e.setAttribute("value", value);
227 n.removeChild(params.item(j));
233 if (e.hasAttribute("in") == false && e.hasAttribute("out") == false) continue;
234 int in = e.attribute("in").toInt();
235 int out = e.attribute("out").toInt();
237 // invalid producer, remove it
238 QString id = e.attribute("id");
239 m_invalidProducers.append(id);
240 m_documentErrors.append(i18n("Invalid clip producer %1\n", id));
241 doc.documentElement().removeChild(producers.at(i));
247 for (int i = 0; i < m_projectTracks; i++) {
248 e = tracks.item(i).toElement();
249 QString playlist_name = e.attribute("producer");
250 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
251 // find playlist related to this track
253 for (int j = 0; j < m_projectTracks; j++) {
254 p = playlists.item(j).toElement();
255 if (p.attribute("id") == playlist_name) {
256 // playlist found, check track effects
257 QDomNodeList trackEffects = p.childNodes();
258 slotAddProjectEffects(trackEffects, p, NULL, trackIndex++);
262 if (p.attribute("id") != playlist_name) { // then it didn't work.
263 kDebug() << "NO PLAYLIST FOUND FOR TRACK " + pos;
265 if (e.attribute("hide") == "video") {
266 m_doc->switchTrackVideo(i - 1, true);
267 } else if (e.attribute("hide") == "audio") {
268 m_doc->switchTrackAudio(i - 1, true);
269 } else if (e.attribute("hide") == "both") {
270 m_doc->switchTrackVideo(i - 1, true);
271 m_doc->switchTrackAudio(i - 1, true);
274 trackduration = slotAddProjectTrack(pos, p, m_doc->isTrackLocked(i - 1));
276 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
277 if (trackduration > duration) duration = trackduration;
279 // background black track
280 for (int j = 0; j < m_projectTracks; j++) {
281 p = playlists.item(j).toElement();
282 if (p.attribute("id") == playlist_name) break;
289 QDomNodeList transitions = tractor.elementsByTagName("transition");
291 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
292 for (int i = 0; i < transitions.count(); i++) {
293 e = transitions.item(i).toElement();
294 QDomNodeList transitionparams = e.childNodes();
295 bool transitionAdd = true;
298 bool isAutomatic = false;
299 bool forceTrack = false;
300 QString mlt_geometry;
302 QString transitionId;
303 for (int k = 0; k < transitionparams.count(); k++) {
304 p = transitionparams.item(k).toElement();
306 QString paramName = p.attribute("name");
307 // do not add audio mixing transitions
308 if (paramName == "internal_added" && p.text() == "237") {
309 transitionAdd = false;
310 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
312 } else if (paramName == "a_track") {
313 a_track = qMax(0, p.text().toInt());
314 a_track = qMin(m_projectTracks - 1, a_track);
315 if (a_track != p.text().toInt()) {
316 // the transition track was out of bounds
317 m_documentErrors.append(i18n("Transition %1 had an invalid track: %2 > %3", e.attribute("id"), p.text().toInt(), a_track) + '\n');
318 EffectsList::setProperty(e, "a_track", QString::number(a_track));
320 } else if (paramName == "b_track") {
321 b_track = qMax(0, p.text().toInt());
322 b_track = qMin(m_projectTracks - 1, b_track);
323 if (b_track != p.text().toInt()) {
324 // the transition track was out of bounds
325 m_documentErrors.append(i18n("Transition %1 had an invalid track: %2 > %3", e.attribute("id"), p.text().toInt(), b_track) + '\n');
326 EffectsList::setProperty(e, "b_track", QString::number(b_track));
328 } else if (paramName == "mlt_service") mlt_service = p.text();
329 else if (paramName == "kdenlive_id") transitionId = p.text();
330 else if (paramName == "geometry") mlt_geometry = p.text();
331 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
332 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
335 if (a_track == b_track || b_track == 0) {
336 // invalid transition, remove it
337 m_documentErrors.append(i18n("Removed invalid transition: %1", e.attribute("id")) + '\n');
338 tractor.removeChild(transitions.item(i));
342 if (transitionAdd || mlt_service != "mix") {
343 // Transition should be added to the scene
344 ItemInfo transitionInfo;
345 if (mlt_service == "composite" && transitionId.isEmpty()) {
346 // When adding composite transition, check if it is a wipe transition
347 if (mlt_geometry.count(';') == 1) {
348 mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
349 mlt_geometry.replace(QChar('x'), QChar(','), Qt::CaseInsensitive);
350 QString start = mlt_geometry.section(';', 0, 0);
351 start = start.section(':', 0, 1);
352 start.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
353 QString end = mlt_geometry.section('=', 1, 1);
354 end = end.section(':', 0, 1);
355 end.replace(QChar(':'), QChar(','), Qt::CaseInsensitive);
356 start.append(',' + end);
357 QStringList numbers = start.split(',', QString::SkipEmptyParts);
358 bool isWipeTransition = true;
360 for (int i = 0; i < numbers.size(); ++i) {
361 checkNumber = qAbs(numbers.at(i).toInt());
362 if (checkNumber != 0 && checkNumber != 100) {
363 isWipeTransition = false;
367 if (isWipeTransition) transitionId = "slide";
371 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
373 if (!base.isNull()) for (int k = 0; k < transitionparams.count(); k++) {
374 p = transitionparams.item(k).toElement();
376 QString paramName = p.attribute("name");
377 QString paramValue = p.text();
379 QDomNodeList params = base.elementsByTagName("parameter");
380 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
381 QDomElement e = params.item(i).toElement();
382 if (!e.isNull() && e.attribute("tag") == paramName) {
383 if (e.attribute("type") == "double") {
384 QString factor = e.attribute("factor", "1");
387 if (factor.startsWith('%')) {
388 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
389 } else fact = factor.toDouble();
390 double val = paramValue.toDouble() * fact;
391 paramValue = QString::number(val);
394 e.setAttribute("value", paramValue);
402 doc.appendChild(doc.importNode(base, true));
403 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
405 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
406 transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
407 transitionInfo.track = m_projectTracks - 1 - b_track;
409 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
410 if (transitionInfo.startPos >= transitionInfo.endPos || base.isNull()) {
411 // invalid transition, remove it.
412 m_documentErrors.append(i18n("Removed invalid transition: (%1, %2, %3)", e.attribute("id"), mlt_service, transitionId) + '\n');
413 kDebug() << "///// REMOVED INVALID TRANSITION: " << e.attribute("id");
414 tractor.removeChild(transitions.item(i));
417 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
418 if (forceTrack) tr->setForcedTrack(true, a_track);
419 m_scene->addItem(tr);
420 if (b_track > 0 && m_doc->isTrackLocked(b_track - 1)) {
421 tr->setItemLocked(true);
428 QDomElement infoXml = mlt.firstChildElement("kdenlivedoc");
431 QDomNodeList guides = infoXml.elementsByTagName("guide");
432 for (int i = 0; i < guides.count(); i++) {
433 e = guides.item(i).toElement();
434 const QString comment = e.attribute("comment");
435 const GenTime pos = GenTime(e.attribute("time").toDouble());
436 m_trackview->addGuide(pos, comment);
440 QDomNodeList groups = infoXml.elementsByTagName("group");
441 m_trackview->loadGroups(groups);
442 m_trackview->setDuration(duration);
443 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
445 // Remove Kdenlive extra info from xml doc before sending it to MLT
446 mlt.removeChild(infoXml);
448 slotRebuildTrackHeaders();
449 if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
450 if (infoXml.hasAttribute("upgraded")) {
451 // Our document was upgraded, create a backup copy just in case
452 QString baseFile = m_doc->url().path().section(".kdenlive", 0, 0);
454 QString backupFile = baseFile + "_backup" + QString::number(ct) + ".kdenlive";
455 while (QFile::exists(backupFile)) {
457 backupFile = baseFile + "_backup" + QString::number(ct) + ".kdenlive";
459 if (KIO::NetAccess::file_copy(m_doc->url(), KUrl(backupFile), this))
460 KMessageBox::information(this, i18n("Your project file was upgraded to the latest Kdenlive document version.\nTo make sure you don't lose data, a backup copy called %1 was created.", backupFile));
462 KMessageBox::information(this, i18n("Your project file was upgraded to the latest Kdenlive document version, but it was not possible to create a backup copy.", backupFile));
464 //m_trackview->setCursorPos(cursorPos);
465 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
468 void TrackView::slotDeleteClip(const QString &clipId)
470 m_trackview->deleteClip(clipId);
473 void TrackView::setCursorPos(int pos)
475 m_trackview->setCursorPos(pos);
478 void TrackView::moveCursorPos(int pos)
480 m_trackview->setCursorPos(pos, false);
483 void TrackView::slotChangeZoom(int horizontal, int vertical)
485 m_ruler->setPixelPerMark(horizontal);
486 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[horizontal];
488 if (vertical == -1) {
490 m_doc->setZoom(horizontal, m_verticalZoom);
491 m_trackview->setScale(m_scale, m_scene->scale().y());
493 m_verticalZoom = vertical;
494 if (m_verticalZoom == 0)
495 m_trackview->setScale(m_scale, 0.5);
497 m_trackview->setScale(m_scale, m_verticalZoom);
498 adjustTrackHeaders();
502 int TrackView::fitZoom() const
504 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
506 for (i = 0; i < 13; i++)
507 if (m_ruler->comboScale[i] > zoom) break;
511 KdenliveDoc *TrackView::document()
516 void TrackView::refresh()
518 m_trackview->viewport()->update();
521 void TrackView::slotRepaintTracks()
523 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
524 for (int i = 0; i < widgets.count(); i++) {
525 if (widgets.at(i)) widgets.at(i)->setSelectedIndex(m_trackview->selectedTrack());
529 void TrackView::slotReloadTracks()
531 slotRebuildTrackHeaders();
532 emit updateTracksInfo();
535 void TrackView::slotRebuildTrackHeaders()
537 const QList <TrackInfo> list = m_doc->tracksList();
539 while ((child = headers_container->layout()->takeAt(0)) != 0) {
540 QWidget *wid = child->widget();
542 if (wid) wid->deleteLater();
544 int max = list.count();
545 int height = KdenliveSettings::trackheight() * m_scene->scale().y() - 1;
546 HeaderTrack *header = NULL;
547 QFrame *frame = NULL;
548 for (int i = 0; i < max; i++) {
549 frame = new QFrame(headers_container);
550 frame->setFrameStyle(QFrame::HLine);
551 frame->setFixedHeight(1);
552 headers_container->layout()->addWidget(frame);
553 TrackInfo info = list.at(max - i - 1);
554 header = new HeaderTrack(i, info, height, headers_container);
555 header->setSelectedIndex(m_trackview->selectedTrack());
556 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
557 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
558 connect(header, SIGNAL(switchTrackLock(int)), m_trackview, SLOT(slotSwitchTrackLock(int)));
559 connect(header, SIGNAL(selectTrack(int)), m_trackview, SLOT(slotSelectTrack(int)));
560 connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
561 connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
562 connect(header, SIGNAL(renameTrack(int, QString)), this, SLOT(slotRenameTrack(int, QString)));
563 connect(header, SIGNAL(configTrack(int)), this, SIGNAL(configTrack(int)));
564 connect(header, SIGNAL(addTrackInfo(const QDomElement, int)), m_trackview, SLOT(slotAddTrackEffect(const QDomElement, int)));
565 connect(header, SIGNAL(showTrackEffects(int)), this, SLOT(slotShowTrackEffects(int)));
566 headers_container->layout()->addWidget(header);
568 frame = new QFrame(this);
569 frame->setFrameStyle(QFrame::HLine);
570 frame->setFixedHeight(1);
571 headers_container->layout()->addWidget(frame);
575 void TrackView::adjustTrackHeaders()
577 int height = KdenliveSettings::trackheight() * m_scene->scale().y() - 1;
578 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
579 for (int i = 0; i < widgets.count(); i++) {
580 if (widgets.at(i)) widgets.at(i)->adjustSize(height);
584 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool locked)
588 QDomNodeList children = xml.childNodes();
589 for (int nodeindex = 0; nodeindex < children.count(); nodeindex++) {
590 QDomNode n = children.item(nodeindex);
591 QDomElement elem = n.toElement();
592 if (elem.tagName() == "blank") {
593 position += elem.attribute("length").toInt();
594 } else if (elem.tagName() == "entry") {
596 int in = elem.attribute("in").toInt();
597 int out = elem.attribute("out").toInt();
598 if (in > out || /*in == out ||*/ m_invalidProducers.contains(elem.attribute("producer"))) {
599 m_documentErrors.append(i18n("Invalid clip removed from track %1 at %2\n", ix, position));
600 xml.removeChild(children.at(nodeindex));
604 QString idString = elem.attribute("producer");
605 QString id = idString;
608 if (idString.startsWith("slowmotion")) {
609 id = idString.section(':', 1, 1);
610 speed = idString.section(':', 2, 2).toDouble();
611 strobe = idString.section(':', 3, 3).toInt();
612 if (strobe == 0) strobe = 1;
613 } else id = id.section('_', 0, 0);
614 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
616 // The clip in playlist was not listed in the kdenlive producers,
617 // something went wrong, repair required.
618 kWarning() << "CANNOT INSERT CLIP " << id;
620 clip = getMissingProducer(id);
622 // We cannot find the producer, something is really wrong, add
623 // placeholder color clip
625 QDomElement producerXml = doc.createElement("producer");
626 doc.appendChild(producerXml);
627 producerXml.setAttribute("colour", "0xff0000ff");
628 producerXml.setAttribute("mlt_service", "colour");
629 producerXml.setAttribute("length", "15000");
630 producerXml.setAttribute("name", "INVALID");
631 producerXml.setAttribute("type", COLOR);
632 producerXml.setAttribute("id", id);
633 clip = new DocClipBase(m_doc->clipManager(), doc.documentElement(), id);
634 xml.insertBefore(producerXml, QDomNode());
635 m_doc->clipManager()->addClip(clip);
637 m_documentErrors.append(i18n("Broken clip producer %1", id) + '\n');
639 // Found correct producer
640 m_documentErrors.append(i18n("Replaced wrong clip producer %1 with %2", id, clip->getId()) + '\n');
641 elem.setAttribute("producer", clip->getId());
643 m_doc->setModified(true);
648 clipinfo.startPos = GenTime(position, m_doc->fps());
649 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
650 clipinfo.cropStart = GenTime(in, m_doc->fps());
651 clipinfo.cropDuration = clipinfo.endPos - clipinfo.startPos;
654 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
655 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), speed, strobe, false);
656 if (idString.endsWith("_video")) item->setVideoOnly(true);
657 else if (idString.endsWith("_audio")) item->setAudioOnly(true);
658 m_scene->addItem(item);
659 if (locked) item->setItemLocked(true);
660 clip->addReference();
661 position += (out - in + 1);
662 if (speed != 1.0 || strobe > 1) {
663 QDomElement speedeffect = MainWindow::videoEffects.getEffectByTag(QString(), "speed").cloneNode().toElement();
664 EffectsList::setParameter(speedeffect, "speed", QString::number((int)(100 * speed + 0.5)));
665 EffectsList::setParameter(speedeffect, "strobe", QString::number(strobe));
666 item->addEffect(speedeffect, false);
667 item->effectsCounter();
670 // parse clip effects
671 QDomNodeList effects = elem.elementsByTagName("filter");
672 slotAddProjectEffects(effects, elem, item, -1);
676 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
680 void TrackView::slotAddProjectEffects(QDomNodeList effects, QDomElement parentNode, ClipItem *clip, int trackIndex)
682 for (int ix = 0; ix < effects.count(); ix++) {
683 bool disableeffect = false;
684 QDomElement effect = effects.at(ix).toElement();
685 if (effect.tagName() != "filter") continue;
687 // add effect to clip
690 QString effectindex = QString::number(ix + 1);
691 QString ladspaEffectFile;
692 // Get effect tag & index
693 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
694 // parse effect parameters
695 QDomElement effectparam = n3.toElement();
696 if (effectparam.attribute("name") == "tag") {
697 effecttag = effectparam.text();
698 } else if (effectparam.attribute("name") == "kdenlive_id") {
699 effectid = effectparam.text();
700 } else if (effectparam.attribute("name") == "disable" && effectparam.text().toInt() == 1) {
702 disableeffect = true;
703 } else if (effectparam.attribute("name") == "kdenlive_ix") {
705 effectparam.firstChild().setNodeValue(effectindex);
706 } else if (effectparam.attribute("name") == "src") {
707 ladspaEffectFile = effectparam.text();
708 if (!QFile::exists(ladspaEffectFile)) {
709 // If the ladspa effect file is missing, recreate it
710 kDebug() << "// MISSING LADSPA FILE: " << ladspaEffectFile;
711 ladspaEffectFile = m_doc->getLadspaFile();
712 effectparam.firstChild().setNodeValue(ladspaEffectFile);
713 kDebug() << "// ... REPLACED WITH: " << ladspaEffectFile;
717 //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
718 // get effect standard tags
719 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
720 if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
721 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
722 if (clipeffect.isNull()) {
723 kDebug() << "/// WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
724 m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
725 if (parentNode.removeChild(effects.at(ix)).isNull()) kDebug() << "/// PROBLEM REMOVING EFFECT: " << effecttag;
728 QDomElement currenteffect = clipeffect.cloneNode().toElement();
729 currenteffect.setAttribute("kdenlive_ix", effectindex);
730 QDomNodeList clipeffectparams = currenteffect.childNodes();
732 if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
733 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
734 // effect is key-framable, read all effects to retrieve keyframes
738 QDomNodeList params = currenteffect.elementsByTagName("parameter");
739 for (int i = 0; i < params.count(); i++) {
740 QDomElement e = params.item(i).toElement();
741 if (e.attribute("type") == "keyframe") {
742 starttag = e.attribute("starttag", "start");
743 endtag = e.attribute("endtag", "end");
744 factor = e.attribute("factor", "1");
749 int effectin = effect.attribute("in").toInt();
750 int effectout = effect.attribute("out").toInt();
751 double startvalue = 0;
754 if (factor.isEmpty()) fact = 1;
755 else if (factor.startsWith('%')) {
756 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
757 } else fact = factor.toDouble();
758 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
759 // parse effect parameters
760 QDomElement effectparam = n3.toElement();
761 if (effectparam.attribute("name") == starttag)
762 startvalue = effectparam.text().toDouble() * fact;
763 if (effectparam.attribute("name") == endtag)
764 endvalue = effectparam.text().toDouble() * fact;
766 // add first keyframe
767 if (effectout <= effectin) {
768 // there is only one keyframe
769 keyframes.append(QString::number(effectin) + ':' + QString::number(startvalue) + ';');
770 } else keyframes.append(QString::number(effectin) + ':' + QString::number(startvalue) + ';' + QString::number(effectout) + ':' + QString::number(endvalue) + ';');
771 QDomNode lastParsedEffect;
773 QDomNode n2 = effects.at(ix);
774 bool continueParsing = true;
775 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
777 QDomElement kfreffect = n2.toElement();
778 int effectout = kfreffect.attribute("out").toInt();
780 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
781 // parse effect parameters
782 QDomElement subeffectparam = n4.toElement();
783 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
784 //We are not in the same effect, stop parsing
785 lastParsedEffect = n2.previousSibling();
787 continueParsing = false;
789 } else if (subeffectparam.attribute("name") == endtag) {
790 endvalue = subeffectparam.text().toDouble() * fact;
794 if (continueParsing) {
795 keyframes.append(QString::number(effectout) + ':' + QString::number(endvalue) + ';');
800 params = currenteffect.elementsByTagName("parameter");
801 for (int i = 0; i < params.count(); i++) {
802 QDomElement e = params.item(i).toElement();
803 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
805 if (!continueParsing) {
806 n2 = lastParsedEffect;
809 // Check if effect has in/out points
810 if (effect.hasAttribute("in")) {
811 EffectsList::setParameter(currenteffect, "in", effect.attribute("in"));
813 if (effect.hasAttribute("out")) {
814 EffectsList::setParameter(currenteffect, "out", effect.attribute("out"));
818 // adjust effect parameters
819 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
820 // parse effect parameters
821 QDomElement effectparam = n3.toElement();
822 QString paramname = effectparam.attribute("name");
823 QString paramvalue = effectparam.text();
825 // try to find this parameter in the effect xml
827 for (int k = 0; k < clipeffectparams.count(); k++) {
828 e = clipeffectparams.item(k).toElement();
829 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
830 if (e.attribute("factor", "1") != "1") {
831 QString factor = e.attribute("factor", "1");
833 if (factor.startsWith('%')) {
834 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
835 } else fact = factor.toDouble();
836 if (e.attribute("type") == "simplekeyframe") {
837 QStringList kfrs = paramvalue.split(";");
838 for (int l = 0; l < kfrs.count(); l++) {
839 QString fr = kfrs.at(l).section('=', 0, 0);
840 double val = kfrs.at(l).section('=', 1, 1).toDouble();
841 kfrs[l] = fr + ":" + QString::number((int)(val * fact));
843 e.setAttribute("keyframes", kfrs.join(";"));
844 } else e.setAttribute("value", paramvalue.toDouble() * fact);
845 } else e.setAttribute("value", paramvalue);
850 if (effecttag == "ladspa") {
851 //QString ladspaEffectFile = EffectsList::parameter(effect, "src", "property");
853 if (!QFile::exists(ladspaEffectFile)) {
854 // If the ladspa effect file is missing, recreate it
855 initEffects::ladspaEffectFile(ladspaEffectFile, currenteffect.attribute("ladspaid").toInt(), m_trackview->getLadspaParams(currenteffect));
857 currenteffect.setAttribute("src", ladspaEffectFile);
859 if (disableeffect) currenteffect.setAttribute("disable", "1");
861 clip->addEffect(currenteffect, false);
863 m_doc->addTrackEffect(trackIndex, currenteffect);
869 DocClipBase *TrackView::getMissingProducer(const QString id) const
871 QDomElement missingXml;
872 QDomDocument doc = m_doc->toXml();
873 QString docRoot = doc.documentElement().attribute("root");
874 if (!docRoot.endsWith('/')) docRoot.append('/');
875 QDomNodeList prods = doc.elementsByTagName("producer");
876 int maxprod = prods.count();
877 for (int i = 0; i < maxprod; i++) {
878 QDomNode m = prods.at(i);
879 QString prodId = m.toElement().attribute("id");
881 missingXml = m.toElement();
885 if (missingXml == QDomElement()) return NULL;
887 QDomNodeList params = missingXml.childNodes();
889 for (int j = 0; j < params.count(); j++) {
890 QDomElement e = params.item(j).toElement();
891 if (e.attribute("name") == "resource") {
892 resource = e.firstChild().nodeValue();
896 // prepend MLT XML document root if no path in clip resource and not a color clip
897 if (!resource.startsWith('/') && !resource.startsWith("0x")) resource.prepend(docRoot);
898 DocClipBase *missingClip = NULL;
899 if (!resource.isEmpty()) {
900 QList <DocClipBase *> list = m_doc->clipManager()->getClipByResource(resource);
901 if (!list.isEmpty()) missingClip = list.at(0);
906 QGraphicsScene *TrackView::projectScene()
911 CustomTrackView *TrackView::projectView()
916 void TrackView::setEditMode(const QString & editMode)
918 m_editMode = editMode;
921 const QString & TrackView::editMode() const
926 void TrackView::slotChangeTrackLock(int ix, bool lock)
928 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
929 widgets.at(ix)->setLock(lock);
933 void TrackView::slotVerticalZoomDown()
935 if (m_verticalZoom == 0) return;
937 m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
938 if (m_verticalZoom == 0)
939 m_trackview->setScale(m_scene->scale().x(), 0.5);
941 m_trackview->setScale(m_scene->scale().x(), 1);
942 adjustTrackHeaders();
943 m_trackview->verticalScrollBar()->setValue(headers_area->verticalScrollBar()->value());
946 void TrackView::slotVerticalZoomUp()
948 if (m_verticalZoom == 2) return;
950 m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
951 if (m_verticalZoom == 2)
952 m_trackview->setScale(m_scene->scale().x(), 2);
954 m_trackview->setScale(m_scene->scale().x(), 1);
955 adjustTrackHeaders();
956 m_trackview->verticalScrollBar()->setValue(headers_area->verticalScrollBar()->value());
959 void TrackView::updateProjectFps()
961 m_ruler->updateProjectFps(m_doc->timecode());
962 m_trackview->updateProjectFps();
965 void TrackView::slotRenameTrack(int ix, QString name)
967 int tracknumber = m_doc->tracksCount() - ix;
968 QList <TrackInfo> tracks = m_doc->tracksList();
969 tracks[tracknumber - 1].trackName = name;
970 ConfigTracksCommand *configTracks = new ConfigTracksCommand(m_trackview, m_doc->tracksList(), tracks);
971 m_doc->commandStack()->push(configTracks);
972 m_doc->setModified(true);
975 void TrackView::slotUpdateVerticalScroll(int /*min*/, int max)
978 if (max > 0) height = m_trackview->horizontalScrollBar()->height() - 1;
979 headers_container->layout()->setContentsMargins(0, m_trackview->frameWidth(), 0, height);
982 void TrackView::updateRuler()
987 void TrackView::slotShowTrackEffects(int ix)
989 m_trackview->clearSelection();
990 emit showTrackEffects(m_doc->tracksCount() - ix, m_doc->trackInfoAt(m_doc->tracksCount() - ix - 1));
993 void TrackView::slotUpdateTrackEffectState(int ix)
995 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
996 if (ix < 0 || ix >= widgets.count()) {
997 kDebug() << "ERROR, Trying to access a non existant track: " << ix;
1000 widgets.at(m_doc->tracksCount() - ix - 1)->updateEffectLabel(m_doc->trackInfoAt(ix).effectsList.effectNames());
1003 #include "trackview.moc"