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 ***************************************************************************/
25 #include "definitions.h"
26 #include "headertrack.h"
27 #include "trackview.h"
29 #include "transition.h"
30 #include "kdenlivesettings.h"
31 #include "clipmanager.h"
32 #include "customruler.h"
33 #include "kdenlivedoc.h"
34 #include "mainwindow.h"
35 #include "customtrackview.h"
37 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
38 : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0) {
40 view = new Ui::TimeLine_UI();
43 m_scene = new QGraphicsScene();
44 m_trackview = new CustomTrackView(doc, m_scene, parent);
45 m_trackview->scale(1, 1);
46 m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
47 //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
49 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
50 QHBoxLayout *layout = new QHBoxLayout;
51 view->ruler_frame->setLayout(layout);
54 layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
55 layout->setContentsMargins(left_margin, 0, right_margin, 0);
56 layout->addWidget(m_ruler);
58 QHBoxLayout *tracksLayout = new QHBoxLayout;
59 tracksLayout->setContentsMargins(0, 0, 0, 0);
60 view->tracks_frame->setLayout(tracksLayout);
62 view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 m_headersLayout = new QVBoxLayout;
66 m_headersLayout->setContentsMargins(0, 0, 0, 0);
67 m_headersLayout->setSpacing(0);
68 view->headers_container->setLayout(m_headersLayout);
70 connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
72 tracksLayout->addWidget(m_trackview);
74 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
75 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
77 parseDocument(m_doc->toXml());
79 connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
80 connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
81 connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
82 connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
83 connect(m_trackview, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotTransitionItemSelected(Transition*)));
84 slotChangeZoom(m_doc->zoom());
88 int TrackView::duration() const {
89 return m_trackview->duration();
92 int TrackView::tracksNumber() const {
93 return m_projectTracks;
96 int TrackView::inPoint() const {
97 return m_ruler->inPoint();
100 int TrackView::outPoint() const {
101 return m_ruler->outPoint();
104 void TrackView::slotClipItemSelected(ClipItem*c) {
105 emit clipItemSelected(c);
108 void TrackView::slotTransitionItemSelected(Transition *t) {
109 emit transitionItemSelected(t);
112 void TrackView::setDuration(int dur) {
113 m_trackview->setDuration(dur);
114 m_ruler->setDuration(dur);
117 void TrackView::parseDocument(QDomDocument doc) {
119 // kDebug() << "//// DOCUMENT: " << doc.toString();
120 QDomNode props = doc.elementsByTagName("properties").item(0);
121 if (!props.isNull()) {
122 cursorPos = props.toElement().attribute("timeline_position").toInt();
125 // parse project tracks
126 QDomNodeList tracks = doc.elementsByTagName("track");
127 QDomNodeList playlists = doc.elementsByTagName("playlist");
129 m_projectTracks = tracks.count();
130 int trackduration = 0;
135 int pos = m_projectTracks - 1;
137 for (int i = 0; i < m_projectTracks; i++) {
138 e = tracks.item(i).toElement();
139 QString playlist_name = e.attribute("producer");
140 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
141 // find playlist related to this track
143 for (int j = 0; j < m_projectTracks; j++) {
144 p = playlists.item(j).toElement();
145 if (p.attribute("id") == playlist_name) break;
147 videotrack = (e.attribute("hide") != "video");
148 trackduration = slotAddProjectTrack(pos, p, videotrack);
150 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
151 if (trackduration > duration) duration = trackduration;
153 // background black track
154 for (int j = 0; j < m_projectTracks; j++) {
155 p = playlists.item(j).toElement();
156 if (p.attribute("id") == playlist_name) break;
158 int black_clips = p.childNodes().count();
159 for (int i = 0; i < black_clips; i++)
160 m_doc->loadingProgressed();
161 qApp->processEvents();
167 QDomNodeList transitions = doc.elementsByTagName("transition");
168 int projectTransitions = transitions.count();
169 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
170 for (int i = 0; i < projectTransitions; i++) {
171 e = transitions.item(i).toElement();
172 QDomNodeList transitionparams = e.childNodes();
173 bool transitionAdd = true;
177 for (int k = 0; k < transitionparams.count(); k++) {
178 p = transitionparams.item(k).toElement();
180 QString paramName = p.attribute("name");
181 // do not add audio mixing transitions
182 if (paramName == "internal_added" && p.text() == "237") {
183 transitionAdd = false;
184 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
186 } else if (paramName == "a_track") a_track = p.text().toInt();
187 else if (paramName == "b_track") b_track = m_projectTracks - 1 - p.text().toInt();
188 else if (paramName == "mlt_service") mlt_service = p.text();
192 // Transition should be added to the scene
193 ItemInfo transitionInfo;
194 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, QString());
196 for (int k = 0; k < transitionparams.count(); k++) {
197 p = transitionparams.item(k).toElement();
199 QString paramName = p.attribute("name");
200 QString paramValue = p.text();
202 QDomNodeList params = base.elementsByTagName("parameter");
203 if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
204 QDomElement e = params.item(i).toElement();
205 if (!e.isNull() && e.attribute("tag") == paramName) {
206 if (e.attribute("type") == "double") {
207 QString factor = e.attribute("factor", "1");
209 double val = paramValue.toDouble() * factor.toDouble();
210 paramValue = QString::number(val);
213 e.setAttribute("value", paramValue);
221 doc.appendChild(doc.importNode(base, true));
222 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
224 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
225 transitionInfo.endPos = GenTime(e.attribute("out").toInt(), m_doc->fps());
226 transitionInfo.track = b_track;
227 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
228 Transition *tr = new Transition(transitionInfo, a_track, m_scale, m_doc->fps(), base);
229 m_scene->addItem(tr);
234 QDomNodeList guides = doc.elementsByTagName("guide");
235 for (int i = 0; i < guides.count(); i++) {
236 e = guides.item(i).toElement();
237 const QString comment = e.attribute("comment");
238 const GenTime pos = GenTime(e.attribute("time").toDouble());
239 m_trackview->addGuide(pos, comment);
242 m_trackview->setDuration(duration);
243 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
244 slotRebuildTrackHeaders();
245 //m_trackview->setCursorPos(cursorPos);
246 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
249 void TrackView::slotDeleteClip(int clipId) {
250 m_trackview->deleteClip(clipId);
253 void TrackView::setCursorPos(int pos) {
254 m_trackview->setCursorPos(pos);
257 void TrackView::moveCursorPos(int pos) {
258 m_trackview->setCursorPos(pos, false);
261 void TrackView::slotChangeZoom(int factor) {
262 m_doc->setZoom(factor);
263 m_ruler->setPixelPerMark(factor);
264 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
265 m_trackview->setScale(m_scale);
268 int TrackView::fitZoom() const {
269 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
271 for (i = 0; i < 13; i++)
272 if (m_ruler->comboScale[i] > zoom) break;
276 const double TrackView::zoomFactor() const {
280 const int TrackView::mapLocalToValue(int x) const {
281 return (int)(x * zoomFactor());
284 KdenliveDoc *TrackView::document() {
288 void TrackView::refresh() {
289 m_trackview->viewport()->update();
292 void TrackView::slotRebuildTrackHeaders() {
293 QList <TrackInfo> list = m_trackview->tracksList();
294 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
295 for (int i = 0; i < widgets.count(); i++)
296 delete widgets.at(i);
297 int max = list.count();
298 for (int i = 0; i < max; i++) {
299 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
300 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
301 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
302 m_headersLayout->addWidget(header);
304 view->headers_container->adjustSize();
307 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
311 info.type = VIDEOTRACK;
313 info.isBlind = false;
315 info.type = AUDIOTRACK;
317 info.isBlind = false;
320 m_trackview->addTrack(info);
322 int trackTop = KdenliveSettings::trackheight() * ix;
325 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
326 QDomElement elem = n.toElement();
327 if (elem.tagName() == "blank") {
328 position += elem.attribute("length").toInt();
329 } else if (elem.tagName() == "entry") {
330 m_doc->loadingProgressed();
331 qApp->processEvents();
333 int in = elem.attribute("in").toInt();
334 int id = elem.attribute("producer").toInt();
335 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
337 int out = elem.attribute("out").toInt();
340 clipinfo.startPos = GenTime(position, m_doc->fps());
341 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
342 clipinfo.cropStart = GenTime(in, m_doc->fps());
344 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
345 ClipItem *item = new ClipItem(clip, clipinfo, m_scale, m_doc->fps());
346 m_scene->addItem(item);
347 clip->addReference();
348 position += (out - in + 1);
350 // parse clip effects
351 for (QDomNode n2 = elem.firstChild(); !n2.isNull(); n2 = n2.nextSibling()) {
352 QDomElement effect = n2.toElement();
353 if (effect.tagName() == "filter") {
354 kDebug() << " * * * * * * * * * * ** CLIP EFF FND * * * * * * * * * * *";
355 // add effect to clip
359 // Get effect tag & index
360 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
361 // parse effect parameters
362 QDomElement effectparam = n3.toElement();
363 if (effectparam.attribute("name") == "tag") {
364 effecttag = effectparam.text();
365 } else if (effectparam.attribute("name") == "kdenlive_id") {
366 effectid = effectparam.text();
367 } else if (effectparam.attribute("name") == "kdenlive_ix") {
368 effectindex = effectparam.text();
372 // get effect standard tags
373 QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
374 if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
375 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
377 clipeffect.setAttribute("kdenlive_ix", effectindex);
378 QDomNodeList clipeffectparams = clipeffect.childNodes();
380 if (MainWindow::videoEffects.hasKeyFrames(clipeffect)) {
381 kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND * * * * * * * * * * *";
382 // effect is key-framable, read all effects to retrieve keyframes
386 QDomNodeList params = clipeffect.elementsByTagName("parameter");
387 for (int i = 0; i < params.count(); i++) {
388 QDomElement e = params.item(i).toElement();
389 if (e.attribute("type") == "keyframe") {
390 starttag = e.attribute("starttag", "start");
391 endtag = e.attribute("endtag", "end");
392 factor = e.attribute("factor", "1").toDouble();
397 int effectin = effect.attribute("in").toInt();
398 int effectout = effect.attribute("out").toInt();
401 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
402 // parse effect parameters
403 QDomElement effectparam = n3.toElement();
404 if (effectparam.attribute("name") == starttag)
405 startvalue = effectparam.text().toDouble() * factor;
406 if (effectparam.attribute("name") == endtag)
407 endvalue = effectparam.text().toDouble() * factor;
409 // add first keyframe
410 keyframes.append(QString::number(in + effectin) + ":" + QString::number(startvalue) + ";" + QString::number(in + effectout) + ":" + QString::number(endvalue) + ";");
411 QDomNode lastParsedEffect;
412 n2 = n2.nextSibling();
413 bool continueParsing = true;
414 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
416 QDomElement kfreffect = n2.toElement();
417 int effectout = kfreffect.attribute("out").toInt();
419 for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
420 // parse effect parameters
421 QDomElement subeffectparam = n4.toElement();
422 if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
423 //We are not in the same effect, stop parsing
424 lastParsedEffect = n2.previousSibling();
425 continueParsing = false;
427 } else if (subeffectparam.attribute("name") == endtag) {
428 endvalue = subeffectparam.text().toDouble() * factor;
432 if (continueParsing) keyframes.append(QString::number(in + effectout) + ":" + QString::number(endvalue) + ";");
435 params = clipeffect.elementsByTagName("parameter");
436 for (int i = 0; i < params.count(); i++) {
437 QDomElement e = params.item(i).toElement();
438 if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
440 if (!continueParsing) {
441 n2 = lastParsedEffect;
445 // adjust effect parameters
446 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
447 // parse effect parameters
448 QDomElement effectparam = n3.toElement();
449 QString paramname = effectparam.attribute("name");
450 QString paramvalue = effectparam.text();
452 // try to find this parameter in the effect xml
454 for (int k = 0; k < clipeffectparams.count(); k++) {
455 e = clipeffectparams.item(k).toElement();
456 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
457 e.setAttribute("value", paramvalue);
462 item->addEffect(clipeffect, false);
463 item->effectsCounter();
467 } else kWarning() << "CANNOT INSERT CLIP " << id;
468 //m_clipList.append(clip);
471 //m_trackDuration = position;
474 //documentTracks.insert(ix, track);
475 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
480 QGraphicsScene *TrackView::projectScene() {
484 CustomTrackView *TrackView::projectView() {
488 void TrackView::setEditMode(const QString & editMode) {
489 m_editMode = editMode;
492 const QString & TrackView::editMode() const {
498 #include "trackview.moc"