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