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