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();
192 m_replacementProducerIds.clear();
194 //kDebug() << "//// DOCUMENT: " << doc.toString();
195 /*QDomNode props = doc.elementsByTagName("properties").item(0);
196 if (!props.isNull()) {
197 cursorPos = props.toElement().attribute("timeline_position").toInt();
200 // parse project tracks
201 QDomElement mlt = doc.firstChildElement("mlt");
202 QDomElement tractor = mlt.firstChildElement("tractor");
203 QDomNodeList tracks = tractor.elementsByTagName("track");
204 QDomNodeList playlists = doc.elementsByTagName("playlist");
206 m_projectTracks = tracks.count();
207 int trackduration = 0;
211 int pos = m_projectTracks - 1;
212 m_invalidProducers.clear();
213 QDomNodeList producers = doc.elementsByTagName("producer");
214 for (int i = 0; i < producers.count(); i++) {
215 // Check for invalid producers
216 QDomNode n = producers.item(i);
219 if (e.hasAttribute("in") == false && e.hasAttribute("out") == false) continue;
220 int in = e.attribute("in").toInt();
221 int out = e.attribute("out").toInt();
223 // invalid producer, remove it
224 QString id = e.attribute("id");
225 m_invalidProducers.append(id);
226 m_documentErrors.append(i18n("Invalid clip producer %1\n", id));
227 doc.documentElement().removeChild(producers.at(i));
233 for (int i = 0; i < m_projectTracks; i++) {
234 e = tracks.item(i).toElement();
235 QString playlist_name = e.attribute("producer");
236 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
237 // find playlist related to this track
239 for (int j = 0; j < m_projectTracks; j++) {
240 p = playlists.item(j).toElement();
241 if (p.attribute("id") == playlist_name) {
242 // playlist found, check track effects
243 QDomNodeList trackEffects = p.childNodes();
244 slotAddProjectEffects(trackEffects, p, NULL, trackIndex++);
248 if (p.attribute("id") != playlist_name) { // then it didn't work.
249 kDebug() << "NO PLAYLIST FOUND FOR TRACK " + pos;
251 if (e.attribute("hide") == "video") {
252 m_doc->switchTrackVideo(i - 1, true);
253 } else if (e.attribute("hide") == "audio") {
254 m_doc->switchTrackAudio(i - 1, true);
255 } else if (e.attribute("hide") == "both") {
256 m_doc->switchTrackVideo(i - 1, true);
257 m_doc->switchTrackAudio(i - 1, true);
260 trackduration = slotAddProjectTrack(pos, p, m_doc->isTrackLocked(i - 1), producers);
262 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
263 if (trackduration > duration) duration = trackduration;
265 // background black track
266 for (int j = 0; j < m_projectTracks; j++) {
267 p = playlists.item(j).toElement();
268 if (p.attribute("id") == playlist_name) break;
275 QDomNodeList transitions = tractor.elementsByTagName("transition");
277 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
278 for (int i = 0; i < transitions.count(); i++) {
279 e = transitions.item(i).toElement();
280 QDomNodeList transitionparams = e.childNodes();
281 bool transitionAdd = true;
284 bool isAutomatic = false;
285 bool forceTrack = false;
286 QString mlt_geometry;
288 QString transitionId;
289 for (int k = 0; k < transitionparams.count(); k++) {
290 p = transitionparams.item(k).toElement();
292 QString paramName = p.attribute("name");
293 // do not add audio mixing transitions
294 if (paramName == "internal_added" && p.text() == "237") {
295 transitionAdd = false;
296 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
298 } else if (paramName == "a_track") {
299 a_track = qMax(0, p.text().toInt());
300 a_track = qMin(m_projectTracks - 1, a_track);
301 if (a_track != p.text().toInt()) {
302 // the transition track was out of bounds
303 m_documentErrors.append(i18n("Transition %1 had an invalid track: %2 > %3", e.attribute("id"), p.text().toInt(), a_track) + '\n');
304 EffectsList::setProperty(e, "a_track", QString::number(a_track));
306 } else if (paramName == "b_track") {
307 b_track = qMax(0, p.text().toInt());
308 b_track = qMin(m_projectTracks - 1, b_track);
309 if (b_track != p.text().toInt()) {
310 // the transition track was out of bounds
311 m_documentErrors.append(i18n("Transition %1 had an invalid track: %2 > %3", e.attribute("id"), p.text().toInt(), b_track) + '\n');
312 EffectsList::setProperty(e, "b_track", QString::number(b_track));
314 } else if (paramName == "mlt_service") mlt_service = p.text();
315 else if (paramName == "kdenlive_id") transitionId = p.text();
316 else if (paramName == "geometry") mlt_geometry = p.text();
317 else if (paramName == "automatic" && p.text() == "1") isAutomatic = true;
318 else if (paramName == "force_track" && p.text() == "1") forceTrack = true;
321 if (a_track == b_track || b_track == 0) {
322 // invalid transition, remove it
323 m_documentErrors.append(i18n("Removed invalid transition: %1", e.attribute("id")) + '\n');
324 tractor.removeChild(transitions.item(i));
328 if (transitionAdd || mlt_service != "mix") {
329 // Transition should be added to the scene
330 ItemInfo transitionInfo;
331 if (mlt_service == "composite" && transitionId.isEmpty()) {
332 // When adding composite transition, check if it is a wipe transition
333 if (mlt_geometry.count(';') == 1) {
334 mlt_geometry.remove(QChar('%'), Qt::CaseInsensitive);
335 mlt_geometry.replace(QChar('x'), QChar(':'), Qt::CaseInsensitive);
336 mlt_geometry.replace(QChar(','), QChar(':'), Qt::CaseInsensitive);
337 mlt_geometry.replace(QChar('/'), QChar(':'), Qt::CaseInsensitive);
339 QString start = mlt_geometry.section('=', 0, 0).section(':', 0, -2) + ':';
340 start.append(mlt_geometry.section('=', 1, 1).section(':', 0, -2));
341 QStringList numbers = start.split(':', QString::SkipEmptyParts);
342 bool isWipeTransition = true;
344 for (int i = 0; i < numbers.size(); ++i) {
345 checkNumber = qAbs(numbers.at(i).toInt());
346 if (checkNumber != 0 && checkNumber != 100) {
347 isWipeTransition = false;
351 if (isWipeTransition) transitionId = "slide";
355 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, transitionId).cloneNode().toElement();
357 if (!base.isNull()) for (int k = 0; k < transitionparams.count(); k++) {
358 p = transitionparams.item(k).toElement();
360 QString paramName = p.attribute("name");
361 QString paramValue = p.text();
363 QDomNodeList params = base.elementsByTagName("parameter");
364 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
365 QDomElement e = params.item(i).toElement();
366 if (!e.isNull() && e.attribute("tag") == paramName) {
367 if (e.attribute("type") == "double") {
368 QString factor = e.attribute("factor", "1");
371 if (factor.contains('%')) {
372 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
373 } else fact = factor.toDouble();
374 double val = paramValue.toDouble() * fact;
375 paramValue = QString::number(val);
378 e.setAttribute("value", paramValue);
386 doc.appendChild(doc.importNode(base, true));
387 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
389 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
390 transitionInfo.endPos = GenTime(e.attribute("out").toInt() + 1, m_doc->fps());
391 transitionInfo.track = m_projectTracks - 1 - b_track;
393 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
394 if (transitionInfo.startPos >= transitionInfo.endPos || base.isNull()) {
395 // invalid transition, remove it.
396 m_documentErrors.append(i18n("Removed invalid transition: (%1, %2, %3)", e.attribute("id"), mlt_service, transitionId) + '\n');
397 kDebug() << "///// REMOVED INVALID TRANSITION: " << e.attribute("id");
398 tractor.removeChild(transitions.item(i));
400 } else if (m_trackview->canBePastedTo(transitionInfo, TRANSITIONWIDGET)) {
401 Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base, isAutomatic);
402 if (forceTrack) tr->setForcedTrack(true, a_track);
403 m_scene->addItem(tr);
404 if (b_track > 0 && m_doc->isTrackLocked(b_track - 1)) {
405 tr->setItemLocked(true);
409 m_documentErrors.append(i18n("Removed overlapping transition: (%1, %2, %3)", e.attribute("id"), mlt_service, transitionId) + '\n');
410 tractor.removeChild(transitions.item(i));
417 QDomElement infoXml = mlt.firstChildElement("kdenlivedoc");
420 QDomNodeList guides = infoXml.elementsByTagName("guide");
421 for (int i = 0; i < guides.count(); i++) {
422 e = guides.item(i).toElement();
423 const QString comment = e.attribute("comment");
424 const GenTime pos = GenTime(e.attribute("time").toDouble());
425 m_trackview->addGuide(pos, comment);
429 QDomNodeList groups = infoXml.elementsByTagName("group");
430 m_trackview->loadGroups(groups);
431 m_trackview->setDuration(duration);
432 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
434 // Remove Kdenlive extra info from xml doc before sending it to MLT
435 mlt.removeChild(infoXml);
437 slotRebuildTrackHeaders();
438 if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
439 if (infoXml.hasAttribute("upgraded") || infoXml.hasAttribute("modified")) {
440 // Our document was upgraded, create a backup copy just in case
441 QString baseFile = m_doc->url().path().section(".kdenlive", 0, 0);
443 QString backupFile = baseFile + "_backup" + QString::number(ct) + ".kdenlive";
444 while (QFile::exists(backupFile)) {
446 backupFile = baseFile + "_backup" + QString::number(ct) + ".kdenlive";
449 if (infoXml.hasAttribute("upgraded"))
450 message = 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);
452 message = i18n("Your project file was modified by Kdenlive.\nTo make sure you don't lose data, a backup copy called %1 was created.", backupFile);
453 if (KIO::NetAccess::file_copy(m_doc->url(), KUrl(backupFile), this))
454 KMessageBox::information(this, message);
456 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));
458 //m_trackview->setCursorPos(cursorPos);
459 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
462 void TrackView::slotDeleteClip(const QString &clipId)
464 m_trackview->deleteClip(clipId);
467 void TrackView::setCursorPos(int pos)
469 m_trackview->setCursorPos(pos);
472 void TrackView::moveCursorPos(int pos)
474 m_trackview->setCursorPos(pos, false);
477 void TrackView::slotChangeZoom(int horizontal, int vertical)
479 m_ruler->setPixelPerMark(horizontal);
480 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[horizontal];
482 if (vertical == -1) {
484 m_doc->setZoom(horizontal, m_verticalZoom);
485 m_trackview->setScale(m_scale, m_scene->scale().y());
487 m_verticalZoom = vertical;
488 if (m_verticalZoom == 0)
489 m_trackview->setScale(m_scale, 0.5);
491 m_trackview->setScale(m_scale, m_verticalZoom);
492 adjustTrackHeaders();
496 int TrackView::fitZoom() const
498 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
500 for (i = 0; i < 13; i++)
501 if (m_ruler->comboScale[i] > zoom) break;
505 KdenliveDoc *TrackView::document()
510 void TrackView::refresh()
512 m_trackview->viewport()->update();
515 void TrackView::slotRepaintTracks()
517 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
518 for (int i = 0; i < widgets.count(); i++) {
519 if (widgets.at(i)) widgets.at(i)->setSelectedIndex(m_trackview->selectedTrack());
523 void TrackView::slotReloadTracks()
525 slotRebuildTrackHeaders();
526 emit updateTracksInfo();
529 void TrackView::slotRebuildTrackHeaders()
531 const QList <TrackInfo> list = m_doc->tracksList();
533 while ((child = headers_container->layout()->takeAt(0)) != 0) {
534 QWidget *wid = child->widget();
536 if (wid) wid->deleteLater();
538 int max = list.count();
539 int height = KdenliveSettings::trackheight() * m_scene->scale().y() - 1;
540 HeaderTrack *header = NULL;
541 QFrame *frame = NULL;
542 for (int i = 0; i < max; i++) {
543 frame = new QFrame(headers_container);
544 frame->setFrameStyle(QFrame::HLine);
545 frame->setFixedHeight(1);
546 headers_container->layout()->addWidget(frame);
547 TrackInfo info = list.at(max - i - 1);
548 header = new HeaderTrack(i, info, height, headers_container);
549 header->setSelectedIndex(m_trackview->selectedTrack());
550 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
551 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
552 connect(header, SIGNAL(switchTrackLock(int)), m_trackview, SLOT(slotSwitchTrackLock(int)));
553 connect(header, SIGNAL(selectTrack(int)), m_trackview, SLOT(slotSelectTrack(int)));
554 connect(header, SIGNAL(deleteTrack(int)), this, SIGNAL(deleteTrack(int)));
555 connect(header, SIGNAL(insertTrack(int)), this, SIGNAL(insertTrack(int)));
556 connect(header, SIGNAL(renameTrack(int, QString)), this, SLOT(slotRenameTrack(int, QString)));
557 connect(header, SIGNAL(configTrack(int)), this, SIGNAL(configTrack(int)));
558 connect(header, SIGNAL(addTrackInfo(const QDomElement, int)), m_trackview, SLOT(slotAddTrackEffect(const QDomElement, int)));
559 connect(header, SIGNAL(showTrackEffects(int)), this, SLOT(slotShowTrackEffects(int)));
560 headers_container->layout()->addWidget(header);
562 frame = new QFrame(this);
563 frame->setFrameStyle(QFrame::HLine);
564 frame->setFixedHeight(1);
565 headers_container->layout()->addWidget(frame);
569 void TrackView::adjustTrackHeaders()
571 int height = KdenliveSettings::trackheight() * m_scene->scale().y() - 1;
572 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
573 for (int i = 0; i < widgets.count(); i++) {
574 if (widgets.at(i)) widgets.at(i)->adjustSize(height);
578 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool locked, QDomNodeList producers)
582 QMap <QString, QString> producerReplacementIds;
583 QDomNodeList children = xml.childNodes();
584 for (int nodeindex = 0; nodeindex < children.count(); nodeindex++) {
585 QDomNode n = children.item(nodeindex);
586 QDomElement elem = n.toElement();
587 if (elem.tagName() == "blank") {
588 position += elem.attribute("length").toInt();
589 } else if (elem.tagName() == "entry") {
591 int in = elem.attribute("in").toInt();
592 int out = elem.attribute("out").toInt();
593 if (in > out || /*in == out ||*/ m_invalidProducers.contains(elem.attribute("producer"))) {
594 m_documentErrors.append(i18n("Invalid clip removed from track %1 at %2\n", ix, position));
595 xml.removeChild(children.at(nodeindex));
599 QString idString = elem.attribute("producer");
600 if (producerReplacementIds.contains(idString)) {
602 elem.setAttribute("producer", producerReplacementIds.value(idString));
603 idString = elem.attribute("producer");
605 QString id = idString;
608 if (idString.startsWith("slowmotion")) {
609 id = idString.section(':', 1, 1);
610 speed = m_locale.toDouble(idString.section(':', 2, 2));
611 strobe = idString.section(':', 3, 3).toInt();
612 if (strobe == 0) strobe = 1;
614 id = id.section('_', 0, 0);
615 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
617 // The clip in playlist was not listed in the kdenlive producers,
618 // something went wrong, repair required.
619 kWarning() << "CANNOT INSERT CLIP " << id;
620 QString docRoot = m_doc->toXml().documentElement().attribute("root");
621 if (!docRoot.endsWith('/')) docRoot.append('/');
622 clip = getMissingProducer(idString);
624 // We found the original producer in Kdenlive's producers
625 // Found correct producer
626 m_documentErrors.append(i18n("Replaced wrong clip producer %1 with %2", id, clip->getId()) + '\n');
627 QString prodId = clip->getId();
628 if (clip->clipType() == PLAYLIST || clip->clipType() == AV || clip->clipType() == AUDIO) {
629 // We need producer for the track
630 prodId.append("_" + QString::number(ix));
632 elem.setAttribute("producer", prodId);
633 producerReplacementIds.insert(idString, prodId);
634 // now adjust the mlt producer
636 for (int i = 0; i < producers.count(); i++) {
637 QDomElement prod = producers.at(i).toElement();
638 if (prod.attribute("id") == prodId) {
639 // ok, producer already exists
645 for (int i = 0; i < producers.count(); i++) {
646 QDomElement prod = producers.at(i).toElement();
647 if (prod.attribute("id") == idString) {
648 prod.setAttribute("id", prodId);
649 m_replacementProducerIds.insert(idString, prodId);
656 // We didn't find the producer for this track, find producer for another track and duplicate
657 for (int i = 0; i < producers.count(); i++) {
658 QDomElement prod = producers.at(i).toElement();
659 QString mltProdId = prod.attribute("id");
660 if (mltProdId == prodId || mltProdId.startsWith(prodId + "_")) {
661 // Found parent producer, clone it
662 QDomElement clone = prod.cloneNode().toElement();
663 clone.setAttribute("id", prodId);
664 m_doc->toXml().documentElement().insertBefore(clone, xml);
671 // We cannot find the producer, something is really wrong, add
672 // placeholder color clip
674 QDomElement producerXml = doc.createElement("producer");
675 doc.appendChild(producerXml);
676 bool foundMltProd = false;
677 for (int i = 0; i < producers.count(); i++) {
678 QDomElement prod = producers.at(i).toElement();
679 if (prod.attribute("id") == id) {
680 QString service = EffectsList::property(prod, "mlt_service");
681 QString type = EffectsList::property(prod, "mlt_type");
682 QString resource = EffectsList::property(prod, "resource");
683 if (!resource.startsWith('/') && service != "colour") {
684 resource.prepend(docRoot);
685 kDebug()<<"******************\nADJUSTED 1\n*************************";
687 QString length = EffectsList::property(prod, "length");
688 producerXml.setAttribute("mlt_service", service);
689 producerXml.setAttribute("mlt_type", type);
690 producerXml.setAttribute("resource", resource);
691 producerXml.setAttribute("duration", length);
692 if (service == "colour") producerXml.setAttribute("type", COLOR);
693 else if (service == "qimage" || service == "pixbuf") producerXml.setAttribute("type", IMAGE);
694 else if (service == "kdenlivetitle") producerXml.setAttribute("type", TEXT);
695 else producerXml.setAttribute("type", AV);
696 clip = new DocClipBase(m_doc->clipManager(), doc.documentElement(), id);
697 m_doc->clipManager()->addClip(clip);
698 m_documentErrors.append(i18n("Broken clip producer %1, recreated base clip: %2", id, resource) + '\n');
704 // Cannot recover, replace with blank
705 int duration = elem.attribute("out").toInt() - elem.attribute("in").toInt();
706 elem.setAttribute("length", duration);
707 elem.setTagName("blank");
708 m_documentErrors.append(i18n("Broken clip producer %1, removed from project", id) + '\n');
711 m_doc->setModified(true);
716 clipinfo.startPos = GenTime(position, m_doc->fps());
717 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
718 clipinfo.cropStart = GenTime(in, m_doc->fps());
719 clipinfo.cropDuration = clipinfo.endPos - clipinfo.startPos;
722 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
723 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps(), speed, strobe, false);
724 if (idString.endsWith("_video")) item->setVideoOnly(true);
725 else if (idString.endsWith("_audio")) item->setAudioOnly(true);
726 m_scene->addItem(item);
727 if (locked) item->setItemLocked(true);
728 clip->addReference();
729 position += (out - in + 1);
730 if (speed != 1.0 || strobe > 1) {
731 QDomElement speedeffect = MainWindow::videoEffects.getEffectByTag(QString(), "speed").cloneNode().toElement();
732 EffectsList::setParameter(speedeffect, "speed", QString::number((int)(100 * speed + 0.5)));
733 EffectsList::setParameter(speedeffect, "strobe", QString::number(strobe));
734 item->addEffect(speedeffect, false);
735 item->effectsCounter();
738 // parse clip effects
739 QDomNodeList effects = elem.elementsByTagName("filter");
740 slotAddProjectEffects(effects, elem, item, -1);
744 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
748 void TrackView::slotAddProjectEffects(QDomNodeList effects, QDomElement parentNode, ClipItem *clip, int trackIndex)
751 for (int ix = 0; ix < effects.count(); ix++) {
752 bool disableeffect = false;
753 QDomElement effect = effects.at(ix).toElement();
754 if (effect.tagName() != "filter") continue;
756 // add effect to clip
759 QString effectindex = QString::number(effectNb);
760 // Get effect tag & index
761 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
762 // parse effect parameters
763 QDomElement effectparam = n3.toElement();
764 if (effectparam.attribute("name") == "tag") {
765 effecttag = effectparam.text();
766 } else if (effectparam.attribute("name") == "kdenlive_id") {
767 effectid = effectparam.text();
768 } else if (effectparam.attribute("name") == "disable" && effectparam.text().toInt() == 1) {
770 disableeffect = true;
771 } else if (effectparam.attribute("name") == "kdenlive_ix") {
773 effectparam.firstChild().setNodeValue(effectindex);
776 //kDebug() << "+ + CLIP EFF FND: " << effecttag << ", " << effectid << ", " << effectindex;
777 // get effect standard tags
778 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
779 if (clipeffect.isNull()) {
780 clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
782 if (clipeffect.isNull()) {
783 clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
785 if (clipeffect.isNull()) {
786 kDebug() << "/// WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
787 m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
788 if (parentNode.removeChild(effects.at(ix)).isNull()) kDebug() << "/// PROBLEM REMOVING EFFECT: " << effecttag;
791 QDomElement currenteffect = clipeffect.cloneNode().toElement();
792 currenteffect.setAttribute("kdenlive_ix", effectindex);
793 QDomNodeList clipeffectparams = currenteffect.childNodes();
795 if (MainWindow::videoEffects.hasKeyFrames(currenteffect)) {
796 //kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
797 // effect is key-framable, read all effects to retrieve keyframes
801 QDomNodeList params = currenteffect.elementsByTagName("parameter");
802 for (int i = 0; i < params.count(); i++) {
803 QDomElement e = params.item(i).toElement();
804 if (e.attribute("type") == "keyframe") {
805 starttag = e.attribute("starttag", "start");
806 endtag = e.attribute("endtag", "end");
807 factor = e.attribute("factor", "1");
812 int effectin = effect.attribute("in").toInt();
813 int effectout = effect.attribute("out").toInt();
814 double startvalue = 0;
817 if (factor.isEmpty()) fact = 1;
818 else if (factor.contains('%')) {
819 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
820 } else fact = factor.toDouble();
821 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
822 // parse effect parameters
823 QDomElement effectparam = n3.toElement();
824 if (effectparam.attribute("name") == starttag)
825 startvalue = effectparam.text().toDouble() * fact;
826 if (effectparam.attribute("name") == endtag)
827 endvalue = effectparam.text().toDouble() * fact;
829 // add first keyframe
830 if (effectout <= effectin) {
831 // there is only one keyframe
832 keyframes.append(QString::number(effectin) + ':' + m_locale.toString(startvalue) + ';');
833 } else keyframes.append(QString::number(effectin) + ':' + m_locale.toString(startvalue) + ';' + QString::number(effectout) + ':' + QString::number(endvalue) + ';');
834 QDomNode lastParsedEffect;
836 QDomNode n2 = effects.at(ix);
837 bool continueParsing = true;
838 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
840 QDomElement kfreffect = n2.toElement();
841 int effectout = kfreffect.attribute("out").toInt();
843 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
844 // parse effect parameters
845 QDomElement subeffectparam = n4.toElement();
846 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
847 //We are not in the same effect, stop parsing
848 lastParsedEffect = n2.previousSibling();
850 continueParsing = false;
852 } else if (subeffectparam.attribute("name") == endtag) {
853 endvalue = subeffectparam.text().toDouble() * fact;
857 if (continueParsing) {
858 keyframes.append(QString::number(effectout) + ':' + m_locale.toString(endvalue) + ';');
863 params = currenteffect.elementsByTagName("parameter");
864 for (int i = 0; i < params.count(); i++) {
865 QDomElement e = params.item(i).toElement();
866 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
868 if (!continueParsing) {
869 n2 = lastParsedEffect;
872 // Check if effect has in/out points
873 if (effect.hasAttribute("in")) {
874 EffectsList::setParameter(currenteffect, "in", effect.attribute("in"));
876 if (effect.hasAttribute("out")) {
877 EffectsList::setParameter(currenteffect, "out", effect.attribute("out"));
881 // adjust effect parameters
882 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
883 // parse effect parameters
884 QDomElement effectparam = n3.toElement();
885 QString paramname = effectparam.attribute("name");
886 QString paramvalue = effectparam.text();
888 // try to find this parameter in the effect xml
890 for (int k = 0; k < clipeffectparams.count(); k++) {
891 e = clipeffectparams.item(k).toElement();
892 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
893 QString type = e.attribute("type");
894 QString factor = e.attribute("factor", "1");
896 if (factor.contains('%')) {
897 fact = ProfilesDialog::getStringEval(m_doc->mltProfile(), factor);
899 fact = factor.toDouble();
901 if (type == "simplekeyframe") {
902 QStringList kfrs = paramvalue.split(";");
903 for (int l = 0; l < kfrs.count(); l++) {
904 QString fr = kfrs.at(l).section('=', 0, 0);
905 double val = m_locale.toDouble(kfrs.at(l).section('=', 1, 1));
906 //kfrs[l] = fr + ":" + m_locale.toString((int)(val * fact));
907 kfrs[l] = fr + ":" + QString::number((int) (val * fact));
909 e.setAttribute("keyframes", kfrs.join(";"));
910 } else if (type == "double" || type == "constant") {
912 e.setAttribute("value", m_locale.toDouble(paramvalue, &ok) * fact);
914 e.setAttribute("value", paramvalue);
916 e.setAttribute("value", paramvalue);
923 if (disableeffect) currenteffect.setAttribute("disable", "1");
925 clip->addEffect(currenteffect, false);
927 m_doc->addTrackEffect(trackIndex, currenteffect);
933 DocClipBase *TrackView::getMissingProducer(const QString id) const
935 QDomElement missingXml;
936 QDomDocument doc = m_doc->toXml();
937 QString docRoot = doc.documentElement().attribute("root");
938 if (!docRoot.endsWith('/')) docRoot.append('/');
939 QDomNodeList prods = doc.elementsByTagName("producer");
940 int maxprod = prods.count();
941 bool slowmotionClip = false;
942 for (int i = 0; i < maxprod; i++) {
943 QDomNode m = prods.at(i);
944 QString prodId = m.toElement().attribute("id");
945 if (prodId.startsWith("slowmotion")) {
946 slowmotionClip = true;
947 prodId = prodId.section(':', 1, 1);
949 prodId = prodId.section('_', 0, 0);
951 missingXml = m.toElement();
955 if (missingXml == QDomElement()) {
956 // Check if producer id was replaced in another track
957 if (m_replacementProducerIds.contains(id)) {
958 QString newId = m_replacementProducerIds.value(id);
959 slowmotionClip = false;
960 for (int i = 0; i < maxprod; i++) {
961 QDomNode m = prods.at(i);
962 QString prodId = m.toElement().attribute("id");
963 if (prodId.startsWith("slowmotion")) {
964 slowmotionClip = true;
965 prodId = prodId.section(':', 1, 1);
967 prodId = prodId.section('_', 0, 0);
969 missingXml = m.toElement();
975 if (missingXml == QDomElement()) return NULL;
976 QString resource = EffectsList::property(missingXml, "resource");
977 QString service = EffectsList::property(missingXml, "mlt_service");
979 if (slowmotionClip) resource = resource.section('?', 0, 0);
980 // prepend MLT XML document root if no path in clip resource and not a color clip
981 if (!resource.startsWith('/') && service != "colour") {
982 resource.prepend(docRoot);
983 kDebug()<<"******************\nADJUSTED 2\n*************************";
985 DocClipBase *missingClip = NULL;
986 if (!resource.isEmpty()) {
987 QList <DocClipBase *> list = m_doc->clipManager()->getClipByResource(resource);
988 if (!list.isEmpty()) missingClip = list.at(0);
993 QGraphicsScene *TrackView::projectScene()
998 CustomTrackView *TrackView::projectView()
1003 void TrackView::setEditMode(const QString & editMode)
1005 m_editMode = editMode;
1008 const QString & TrackView::editMode() const
1013 void TrackView::slotChangeTrackLock(int ix, bool lock)
1015 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
1016 widgets.at(ix)->setLock(lock);
1020 void TrackView::slotVerticalZoomDown()
1022 if (m_verticalZoom == 0) return;
1024 m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
1025 if (m_verticalZoom == 0)
1026 m_trackview->setScale(m_scene->scale().x(), 0.5);
1028 m_trackview->setScale(m_scene->scale().x(), 1);
1029 adjustTrackHeaders();
1030 m_trackview->verticalScrollBar()->setValue(headers_area->verticalScrollBar()->value());
1033 void TrackView::slotVerticalZoomUp()
1035 if (m_verticalZoom == 2) return;
1037 m_doc->setZoom(m_doc->zoom().x(), m_verticalZoom);
1038 if (m_verticalZoom == 2)
1039 m_trackview->setScale(m_scene->scale().x(), 2);
1041 m_trackview->setScale(m_scene->scale().x(), 1);
1042 adjustTrackHeaders();
1043 m_trackview->verticalScrollBar()->setValue(headers_area->verticalScrollBar()->value());
1046 void TrackView::updateProjectFps()
1048 m_ruler->updateProjectFps(m_doc->timecode());
1049 m_trackview->updateProjectFps();
1052 void TrackView::slotRenameTrack(int ix, QString name)
1054 int tracknumber = m_doc->tracksCount() - ix;
1055 QList <TrackInfo> tracks = m_doc->tracksList();
1056 tracks[tracknumber - 1].trackName = name;
1057 ConfigTracksCommand *configTracks = new ConfigTracksCommand(m_trackview, m_doc->tracksList(), tracks);
1058 m_doc->commandStack()->push(configTracks);
1059 m_doc->setModified(true);
1062 void TrackView::slotUpdateVerticalScroll(int /*min*/, int max)
1065 if (max > 0) height = m_trackview->horizontalScrollBar()->height() - 1;
1066 headers_container->layout()->setContentsMargins(0, m_trackview->frameWidth(), 0, height);
1069 void TrackView::updateRuler()
1074 void TrackView::slotShowTrackEffects(int ix)
1076 m_trackview->clearSelection();
1077 emit showTrackEffects(m_doc->tracksCount() - ix, m_doc->trackInfoAt(m_doc->tracksCount() - ix - 1));
1080 void TrackView::slotUpdateTrackEffectState(int ix)
1082 QList<HeaderTrack *> widgets = findChildren<HeaderTrack *>();
1083 if (ix < 0 || ix >= widgets.count()) {
1084 kDebug() << "ERROR, Trying to access a non existant track: " << ix;
1087 widgets.at(m_doc->tracksCount() - ix - 1)->updateEffectLabel(m_doc->trackInfoAt(ix).effectsList.effectNames());
1090 void TrackView::slotSaveTimelinePreview(const QString path)
1092 QImage img(width(), height(), QImage::Format_ARGB32_Premultiplied);
1093 img.fill(palette().base().color().rgb());
1094 QPainter painter(&img);
1097 img = img.scaledToWidth(600, Qt::SmoothTransformation);
1102 #include "trackview.moc"