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