]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
cb094e08413134c2c9c3ef6a42e73f6f98a67bb2
[kdenlive] / src / trackview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QScrollBar>
22
23 #include <KDebug>
24 #include <KMessageBox>
25
26 #include "definitions.h"
27 #include "headertrack.h"
28 #include "trackview.h"
29 #include "clipitem.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
38
39 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
40         : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0) {
41
42     view = new Ui::TimeLine_UI();
43     view->setupUi(this);
44
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));
50
51     m_ruler = new CustomRuler(doc->timecode(), m_trackview);
52     QHBoxLayout *layout = new QHBoxLayout;
53     view->ruler_frame->setLayout(layout);
54     int left_margin;
55     int right_margin;
56     layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
57     layout->setContentsMargins(left_margin, 0, right_margin, 0);
58     layout->addWidget(m_ruler);
59
60     QHBoxLayout *tracksLayout = new QHBoxLayout;
61     tracksLayout->setContentsMargins(0, 0, 0, 0);
62     view->tracks_frame->setLayout(tracksLayout);
63
64     view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65     view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66
67     m_headersLayout = new QVBoxLayout;
68     m_headersLayout->setContentsMargins(0, 0, 0, 0);
69     m_headersLayout->setSpacing(0);
70     view->headers_container->setLayout(m_headersLayout);
71
72     connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
73
74     tracksLayout->addWidget(m_trackview);
75
76     connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
77     connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
78
79     parseDocument(m_doc->toXml());
80
81     connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
82     connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
83     connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
84     connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
85     connect(m_trackview, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotTransitionItemSelected(Transition*)));
86     slotChangeZoom(m_doc->zoom());
87 }
88
89
90 int TrackView::duration() const {
91     return m_trackview->duration();
92 }
93
94 int TrackView::tracksNumber() const {
95     return m_projectTracks;
96 }
97
98 int TrackView::inPoint() const {
99     return m_ruler->inPoint();
100 }
101
102 int TrackView::outPoint() const {
103     return m_ruler->outPoint();
104 }
105
106 void TrackView::slotClipItemSelected(ClipItem*c) {
107     emit clipItemSelected(c);
108 }
109
110 void TrackView::slotTransitionItemSelected(Transition *t) {
111     emit transitionItemSelected(t);
112 }
113
114 void TrackView::setDuration(int dur) {
115     m_trackview->setDuration(dur);
116     m_ruler->setDuration(dur);
117 }
118
119 void TrackView::parseDocument(QDomDocument doc) {
120     int cursorPos = 0;
121     m_documentErrors = QString();
122     // kDebug() << "//// DOCUMENT: " << doc.toString();
123     QDomNode props = doc.elementsByTagName("properties").item(0);
124     if (!props.isNull()) {
125         cursorPos = props.toElement().attribute("timeline_position").toInt();
126     }
127
128     // parse project tracks
129     QDomNodeList tracks = doc.elementsByTagName("track");
130     QDomNodeList playlists = doc.elementsByTagName("playlist");
131     int duration = 300;
132     m_projectTracks = tracks.count();
133     int trackduration = 0;
134     QDomElement e;
135     QDomElement p;
136     bool videotrack;
137
138     int pos = m_projectTracks - 1;
139
140
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
146             p = QDomElement();
147             for (int j = 0; j < m_projectTracks; j++) {
148                 p = playlists.item(j).toElement();
149                 if (p.attribute("id") == playlist_name) break;
150             }
151             videotrack = (e.attribute("hide") != "video");
152             trackduration = slotAddProjectTrack(pos, p, videotrack);
153             pos--;
154             //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
155             if (trackduration > duration) duration = trackduration;
156         } else {
157             // background black track
158             for (int j = 0; j < m_projectTracks; j++) {
159                 p = playlists.item(j).toElement();
160                 if (p.attribute("id") == playlist_name) break;
161             }
162             int black_clips = p.childNodes().count();
163             for (int i = 0; i < black_clips; i++)
164                 m_doc->loadingProgressed();
165             qApp->processEvents();
166             pos--;
167         }
168     }
169
170     // parse transitions
171     QDomNodeList transitions = doc.elementsByTagName("transition");
172     int projectTransitions = transitions.count();
173     //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
174     for (int i = 0; i < projectTransitions; i++) {
175         e = transitions.item(i).toElement();
176         QDomNodeList transitionparams = e.childNodes();
177         bool transitionAdd = true;
178         int a_track = 0;
179         int b_track = 0;
180         QString mlt_service;
181         for (int k = 0; k < transitionparams.count(); k++) {
182             p = transitionparams.item(k).toElement();
183             if (!p.isNull()) {
184                 QString paramName = p.attribute("name");
185                 // do not add audio mixing transitions
186                 if (paramName == "internal_added" && p.text() == "237") {
187                     transitionAdd = false;
188                     //kDebug() << "//  TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
189                     break;
190                 } else if (paramName == "a_track") a_track = p.text().toInt();
191                 else if (paramName == "b_track") b_track = m_projectTracks - 1 - p.text().toInt();
192                 else if (paramName == "mlt_service") mlt_service = p.text();
193             }
194         }
195         if (transitionAdd) {
196             // Transition should be added to the scene
197             ItemInfo transitionInfo;
198             QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, QString());
199
200             for (int k = 0; k < transitionparams.count(); k++) {
201                 p = transitionparams.item(k).toElement();
202                 if (!p.isNull()) {
203                     QString paramName = p.attribute("name");
204                     QString paramValue = p.text();
205
206                     QDomNodeList params = base.elementsByTagName("parameter");
207                     if (paramName != "a_track" && paramName != "b_track") for (int i = 0; i < params.count(); i++) {
208                             QDomElement e = params.item(i).toElement();
209                             if (!e.isNull() && e.attribute("tag") == paramName) {
210                                 if (e.attribute("type") == "double") {
211                                     QString factor = e.attribute("factor", "1");
212                                     if (factor != "1") {
213                                         double val = paramValue.toDouble() * factor.toDouble();
214                                         paramValue = QString::number(val);
215                                     }
216                                 }
217                                 e.setAttribute("value", paramValue);
218                                 break;
219                             }
220                         }
221                 }
222             }
223
224             /*QDomDocument doc;
225             doc.appendChild(doc.importNode(base, true));
226             kDebug() << "///////  TRANSITION XML: "<< doc.toString();*/
227
228             transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
229             transitionInfo.endPos = GenTime(e.attribute("out").toInt(), m_doc->fps());
230             transitionInfo.track = b_track;
231             //kDebug() << "///////////////   +++++++++++  ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
232             Transition *tr = new Transition(transitionInfo, a_track, m_doc->fps(), base);
233             m_scene->addItem(tr);
234         }
235     }
236
237     // Add guides
238     QDomNodeList guides = doc.elementsByTagName("guide");
239     for (int i = 0; i < guides.count(); i++) {
240         e = guides.item(i).toElement();
241         const QString comment = e.attribute("comment");
242         const GenTime pos = GenTime(e.attribute("time").toDouble());
243         m_trackview->addGuide(pos, comment);
244     }
245
246     m_trackview->setDuration(duration);
247     kDebug() << "///////////  TOTAL PROJECT DURATION: " << duration;
248     slotRebuildTrackHeaders();
249     if (!m_documentErrors.isNull()) KMessageBox::sorry(this, m_documentErrors);
250     //m_trackview->setCursorPos(cursorPos);
251     //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
252 }
253
254 void TrackView::slotDeleteClip(int clipId) {
255     m_trackview->deleteClip(clipId);
256 }
257
258 void TrackView::setCursorPos(int pos) {
259     m_trackview->setCursorPos(pos);
260 }
261
262 void TrackView::moveCursorPos(int pos) {
263     m_trackview->setCursorPos(pos, false);
264 }
265
266 void TrackView::slotChangeZoom(int factor) {
267     m_doc->setZoom(factor);
268     m_ruler->setPixelPerMark(factor);
269     m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
270     m_trackview->setScale(m_scale);
271 }
272
273 int TrackView::fitZoom() const {
274     int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
275     int i;
276     for (i = 0; i < 13; i++)
277         if (m_ruler->comboScale[i] > zoom) break;
278     return i;
279 }
280
281 const double TrackView::zoomFactor() const {
282     return m_scale;
283 }
284
285 const int TrackView::mapLocalToValue(int x) const {
286     return (int)(x * zoomFactor());
287 }
288
289 KdenliveDoc *TrackView::document() {
290     return m_doc;
291 }
292
293 void TrackView::refresh() {
294     m_trackview->viewport()->update();
295 }
296
297 void TrackView::slotRebuildTrackHeaders() {
298     QList <TrackInfo> list = m_trackview->tracksList();
299     QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
300     for (int i = 0; i < widgets.count(); i++)
301         delete widgets.at(i);
302     int max = list.count();
303     for (int i = 0; i < max; i++) {
304         HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
305         connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
306         connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
307         m_headersLayout->addWidget(header);
308     }
309     view->headers_container->adjustSize();
310 }
311
312 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
313     TrackInfo info;
314
315     if (videotrack) {
316         info.type = VIDEOTRACK;
317         info.isMute = false;
318         info.isBlind = false;
319     } else {
320         info.type = AUDIOTRACK;
321         info.isMute = false;
322         info.isBlind = false;
323     }
324
325     m_trackview->addTrack(info);
326
327     int trackTop = KdenliveSettings::trackheight() * ix;
328     // parse track
329     int position = 0;
330     for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
331         QDomElement elem = n.toElement();
332         if (elem.tagName() == "blank") {
333             position += elem.attribute("length").toInt();
334         } else if (elem.tagName() == "entry") {
335             m_doc->loadingProgressed();
336             qApp->processEvents();
337             // Found a clip
338             int in = elem.attribute("in").toInt();
339             QString idString = elem.attribute("producer");
340             int id = idString.toInt();
341             bool hasSpeedAttribute = false;
342             double speed;
343             if (idString.startsWith("slowmotion")) {
344                 hasSpeedAttribute = true;
345                 id = idString.section(":", 1, 1).toInt();
346                 speed = idString.section(":", 2, 2).toDouble();
347             }
348             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
349             if (clip != NULL) {
350                 int out = elem.attribute("out").toInt();
351
352                 ItemInfo clipinfo;
353                 clipinfo.startPos = GenTime(position, m_doc->fps());
354                 clipinfo.endPos = clipinfo.startPos + GenTime(out - in + 1, m_doc->fps());
355                 clipinfo.cropStart = GenTime(in, m_doc->fps());
356                 clipinfo.track = ix;
357                 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
358                 ClipItem *item = new ClipItem(clip, clipinfo, m_doc->fps());
359                 if (hasSpeedAttribute) item->setSpeed(speed);
360                 m_scene->addItem(item);
361                 clip->addReference();
362                 position += (out - in + 1);
363
364                 // parse clip effects
365                 QDomNodeList effects = elem.childNodes();
366                 for (int ix = 0; ix < effects.count(); ix++) {
367                     QDomElement effect = effects.at(ix).toElement();
368                     if (effect.tagName() == "filter") {
369                         kDebug() << " * * * * * * * * * * ** CLIP EFF FND  * * * * * * * * * * *";
370                         // add effect to clip
371                         QString effecttag;
372                         QString effectid;
373                         QString effectindex;
374                         // Get effect tag & index
375                         for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
376                             // parse effect parameters
377                             QDomElement effectparam = n3.toElement();
378                             if (effectparam.attribute("name") == "tag") {
379                                 effecttag = effectparam.text();
380                             } else if (effectparam.attribute("name") == "kdenlive_id") {
381                                 effectid = effectparam.text();
382                             } else if (effectparam.attribute("name") == "kdenlive_ix") {
383                                 effectindex = effectparam.text();
384                             }
385                         }
386
387                         // get effect standard tags
388                         QDomElement clipeffect = MainWindow::customEffects.getEffectByTag(QString(), effectid);
389                         if (clipeffect.isNull()) clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
390                         if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
391                         if (clipeffect.isNull()) {
392                             kDebug() << "///  WARNING, EFFECT: " << effecttag << ": " << effectid << " not found, removing it from project";
393                             m_documentErrors.append(i18n("Effect %1:%2 not found in MLT, it was removed from this project\n", effecttag, effectid));
394                             elem.removeChild(effects.at(ix));
395                             ix--;
396                         } else {
397                             clipeffect.setAttribute("kdenlive_ix", effectindex);
398                             QDomNodeList clipeffectparams = clipeffect.childNodes();
399
400                             if (MainWindow::videoEffects.hasKeyFrames(clipeffect)) {
401                                 kDebug() << " * * * * * * * * * * ** CLIP EFF WITH KFR FND  * * * * * * * * * * *";
402                                 // effect is key-framable, read all effects to retrieve keyframes
403                                 double factor;
404                                 QString starttag;
405                                 QString endtag;
406                                 QDomNodeList params = clipeffect.elementsByTagName("parameter");
407                                 for (int i = 0; i < params.count(); i++) {
408                                     QDomElement e = params.item(i).toElement();
409                                     if (e.attribute("type") == "keyframe") {
410                                         starttag = e.attribute("starttag", "start");
411                                         endtag = e.attribute("endtag", "end");
412                                         factor = e.attribute("factor", "1").toDouble();
413                                         break;
414                                     }
415                                 }
416                                 QString keyframes;
417                                 int effectin = effect.attribute("in").toInt();
418                                 int effectout = effect.attribute("out").toInt();
419                                 double startvalue;
420                                 double endvalue;
421                                 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
422                                     // parse effect parameters
423                                     QDomElement effectparam = n3.toElement();
424                                     if (effectparam.attribute("name") == starttag)
425                                         startvalue = effectparam.text().toDouble() * factor;
426                                     if (effectparam.attribute("name") == endtag)
427                                         endvalue = effectparam.text().toDouble() * factor;
428                                 }
429                                 // add first keyframe
430                                 keyframes.append(QString::number(in + effectin) + ":" + QString::number(startvalue) + ";" + QString::number(in + effectout) + ":" + QString::number(endvalue) + ";");
431                                 QDomNode lastParsedEffect;
432                                 ix++;
433                                 QDomNode n2 = effects.at(ix);
434                                 bool continueParsing = true;
435                                 for (; !n2.isNull() && continueParsing; n2 = n2.nextSibling()) {
436                                     // parse all effects
437                                     QDomElement kfreffect = n2.toElement();
438                                     int effectout = kfreffect.attribute("out").toInt();
439
440                                     for (QDomNode n4 = kfreffect.firstChild(); !n4.isNull(); n4 = n4.nextSibling()) {
441                                         // parse effect parameters
442                                         QDomElement subeffectparam = n4.toElement();
443                                         if (subeffectparam.attribute("name") == "kdenlive_ix" && subeffectparam.text() != effectindex) {
444                                             //We are not in the same effect, stop parsing
445                                             lastParsedEffect = n2.previousSibling();
446                                             continueParsing = false;
447                                             break;
448                                         } else if (subeffectparam.attribute("name") == endtag) {
449                                             endvalue = subeffectparam.text().toDouble() * factor;
450                                             break;
451                                         }
452                                     }
453                                     if (continueParsing) keyframes.append(QString::number(in + effectout) + ":" + QString::number(endvalue) + ";");
454                                     ix++;
455                                 }
456
457                                 params = clipeffect.elementsByTagName("parameter");
458                                 for (int i = 0; i < params.count(); i++) {
459                                     QDomElement e = params.item(i).toElement();
460                                     if (e.attribute("type") == "keyframe") e.setAttribute("keyframes", keyframes);
461                                 }
462                                 if (!continueParsing) {
463                                     n2 = lastParsedEffect;
464                                 }
465                             }
466
467                             // adjust effect parameters
468                             for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
469                                 // parse effect parameters
470                                 QDomElement effectparam = n3.toElement();
471                                 QString paramname = effectparam.attribute("name");
472                                 QString paramvalue = effectparam.text();
473
474                                 // try to find this parameter in the effect xml
475                                 QDomElement e;
476                                 for (int k = 0; k < clipeffectparams.count(); k++) {
477                                     e = clipeffectparams.item(k).toElement();
478                                     if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
479                                         e.setAttribute("value", paramvalue);
480                                         break;
481                                     }
482                                 }
483                             }
484                             item->addEffect(clipeffect, false);
485                             item->effectsCounter();
486                         }
487                     }
488                 }
489             } else kWarning() << "CANNOT INSERT CLIP " << id;
490             //m_clipList.append(clip);
491         }
492     }
493     //m_trackDuration = position;
494
495
496     //documentTracks.insert(ix, track);
497     kDebug() << "*************  ADD DOC TRACK " << ix << ", DURATION: " << position;
498     return position;
499     //track->show();
500 }
501
502 QGraphicsScene *TrackView::projectScene() {
503     return m_scene;
504 }
505
506 CustomTrackView *TrackView::projectView() {
507     return m_trackview;
508 }
509
510 void TrackView::setEditMode(const QString & editMode) {
511     m_editMode = editMode;
512 }
513
514 const QString & TrackView::editMode() const {
515     return m_editMode;
516 }
517
518
519
520 #include "trackview.moc"