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