]> git.sesse.net Git - kdenlive/blob - src/trackview.cpp
8117e5eb1f18429e1bbd301e0c7fa8b1733f9095
[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 <QMouseEvent>
22 #include <QStylePainter>
23 #include <QScrollBar>
24
25 #include <KDebug>
26
27 #include "definitions.h"
28 #include "headertrack.h"
29 #include "trackview.h"
30 #include "clipitem.h"
31 #include "transition.h"
32 #include "kdenlivesettings.h"
33 #include "clipmanager.h"
34 #include "customruler.h"
35 #include "kdenlivedoc.h"
36 #include "mainwindow.h"
37 #include "customtrackview.h"
38
39 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
40         : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_currentZoom(4) {
41
42     view = new Ui::TimeLine_UI();
43     view->setupUi(this);
44
45     m_scene = new QGraphicsScene();
46     m_trackview = new CustomTrackView(doc, m_scene, parent);
47     m_trackview->scale(1, 1);
48     m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
49     //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
50
51     m_ruler = new CustomRuler(doc->timecode(), m_trackview);
52     QHBoxLayout *layout = new QHBoxLayout;
53     view->ruler_frame->setLayout(layout);
54     int left_margin;
55     int right_margin;
56     layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
57     layout->setContentsMargins(left_margin, 0, right_margin, 0);
58     layout->addWidget(m_ruler);
59
60     QHBoxLayout *tracksLayout = new QHBoxLayout;
61     tracksLayout->setContentsMargins(0, 0, 0, 0);
62     view->tracks_frame->setLayout(tracksLayout);
63
64     view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65     view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66
67     m_headersLayout = new QVBoxLayout;
68     m_headersLayout->setContentsMargins(0, 0, 0, 0);
69     m_headersLayout->setSpacing(0);
70     view->headers_container->setLayout(m_headersLayout);
71
72     connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
73
74     tracksLayout->addWidget(m_trackview);
75
76     connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
77     connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
78
79     parseDocument(doc->toXml());
80
81     connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
82     connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
83     connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
84     connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
85     connect(m_trackview, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotTransitionItemSelected(Transition*)));
86     slotChangeZoom(m_currentZoom);
87 }
88
89 int TrackView::currentZoom() const {
90     return m_currentZoom;
91 }
92
93 int TrackView::duration() const {
94     return m_trackview->duration();
95 }
96
97 int TrackView::tracksNumber() const {
98     return m_projectTracks;
99 }
100
101 int TrackView::inPoint() const {
102     return m_ruler->inPoint();
103 }
104
105 int TrackView::outPoint() const {
106     return m_ruler->outPoint();
107 }
108
109 void TrackView::slotClipItemSelected(ClipItem*c) {
110     emit clipItemSelected(c);
111 }
112
113 void TrackView::slotTransitionItemSelected(Transition *t) {
114     emit transitionItemSelected(t);
115 }
116
117 void TrackView::setDuration(int dur) {
118     m_trackview->setDuration(dur);
119     m_ruler->setDuration(dur);
120 }
121
122 void TrackView::parseDocument(QDomDocument doc) {
123     int cursorPos = 0;
124     //kDebug() << "//// DOCUMENT: " << doc.toString();
125     QDomNode props = doc.elementsByTagName("properties").item(0);
126     if (!props.isNull()) {
127         cursorPos = props.toElement().attribute("timeline_position").toInt();
128     }
129
130     // parse project tracks
131     QDomNodeList tracks = doc.elementsByTagName("track");
132     QDomNodeList playlists = doc.elementsByTagName("playlist");
133     int duration = 300;
134     m_projectTracks = tracks.count();
135     int trackduration = 0;
136     QDomElement e;
137     QDomElement p;
138     bool videotrack;
139     kDebug() << "//////////// TIMELINE FOUND: " << m_projectTracks << " tracks";
140     int pos = m_projectTracks - 1;
141     for (int i = 0; i < m_projectTracks; i++) {
142         e = tracks.item(i).toElement();
143         QString playlist_name = e.attribute("producer");
144         if (playlist_name != "black_track" && playlist_name != "playlistmain") {
145             // find playlist related to this track
146             p = QDomElement();
147             for (int j = 0; j < m_projectTracks; j++) {
148                 p = playlists.item(j).toElement();
149                 if (p.attribute("id") == playlist_name) break;
150             }
151             videotrack = (e.attribute("hide") != "video");
152             trackduration = slotAddProjectTrack(pos, p, videotrack);
153             pos--;
154             kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
155             if (trackduration > duration) duration = trackduration;
156         } else pos--;
157     }
158
159
160     // parse transitions
161     QDomNodeList transitions = doc.elementsByTagName("transition");
162     int projectTransitions = transitions.count();
163     kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
164     for (int i = 0; i < projectTransitions; i++) {
165         e = transitions.item(i).toElement();
166         QDomNodeList transitionparams = e.childNodes();
167         bool transitionAdd = true;
168         int a_track = 0;
169         int b_track = 0;
170         QString mlt_service;
171         for (int k = 0; k < transitionparams.count(); k++) {
172             p = transitionparams.item(k).toElement();
173             if (!p.isNull()) {
174                 // do not add audio mixing transitions
175                 if (p.attribute("name") == "internal_added" && p.text() == "237") {
176                     transitionAdd = false;
177                     kDebug() << "//  TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
178                     break;
179                 } else if (p.attribute("name") == "a_track") a_track = p.text().toInt();
180                 else if (p.attribute("name") == "b_track") b_track = m_projectTracks - 1 - p.text().toInt();
181                 else if (p.attribute("name") == "mlt_service") mlt_service = p.text();
182             }
183         }
184         if (transitionAdd) {
185             // Transition should be added to the scene
186             ItemInfo transitionInfo;
187             transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
188             transitionInfo.endPos = GenTime(e.attribute("out").toInt(), m_doc->fps());
189             transitionInfo.track = b_track;
190             kDebug() << "///////////////   +++++++++++  ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
191             Transition *tr = new Transition(transitionInfo, a_track, m_scale, m_doc->fps(), QDomElement());
192             m_scene->addItem(tr);
193         }
194     }
195
196
197     m_trackview->setDuration(duration);
198     kDebug() << "///////////  TOTAL PROJECT DURATION: " << duration;
199     slotRebuildTrackHeaders();
200     //m_trackview->setCursorPos(cursorPos);
201     //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
202 }
203
204 void TrackView::slotDeleteClip(int clipId) {
205     m_trackview->deleteClip(clipId);
206 }
207
208 void TrackView::setCursorPos(int pos) {
209     m_trackview->setCursorPos(pos);
210 }
211
212 void TrackView::moveCursorPos(int pos) {
213     m_trackview->setCursorPos(pos, false);
214 }
215
216 void TrackView::slotChangeZoom(int factor) {
217
218     m_ruler->setPixelPerMark(factor);
219     m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
220     m_currentZoom = factor;
221     m_trackview->setScale(m_scale);
222 }
223
224 int TrackView::fitZoom() const {
225     int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
226     int i;
227     for (i = 0; i < 13; i++)
228         if (m_ruler->comboScale[i] > zoom) break;
229     return i;
230 }
231
232 const double TrackView::zoomFactor() const {
233     return m_scale;
234 }
235
236 const int TrackView::mapLocalToValue(int x) const {
237     return (int)(x * zoomFactor());
238 }
239
240 KdenliveDoc *TrackView::document() {
241     return m_doc;
242 }
243
244 void TrackView::refresh() {
245     m_trackview->viewport()->update();
246 }
247
248 void TrackView::slotRebuildTrackHeaders() {
249     QList <TrackInfo> list = m_trackview->tracksList();
250     QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
251     for (int i = 0; i < widgets.count(); i++)
252         delete widgets.at(i);
253     int max = list.count();
254     for (int i = 0; i < max; i++) {
255         HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
256         connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
257         connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
258         m_headersLayout->addWidget(header);
259     }
260     view->headers_container->adjustSize();
261 }
262
263 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
264     TrackInfo info;
265
266     if (videotrack) {
267         info.type = VIDEOTRACK;
268         info.isMute = false;
269         info.isBlind = false;
270     } else {
271         info.type = AUDIOTRACK;
272         info.isMute = false;
273         info.isBlind = false;
274     }
275
276     m_trackview->addTrack(info);
277
278     int trackTop = KdenliveSettings::trackheight() * ix;
279     // parse track
280     int position = 0;
281     for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
282         QDomElement elem = n.toElement();
283         if (elem.tagName() == "blank") {
284             position += elem.attribute("length").toInt();
285         } else if (elem.tagName() == "entry") {
286             // Found a clip
287             int in = elem.attribute("in").toInt();
288             int id = elem.attribute("producer").toInt();
289             DocClipBase *clip = m_doc->clipManager()->getClipById(id);
290             if (clip != NULL) {
291                 int out = elem.attribute("out").toInt();
292
293                 ItemInfo clipinfo;
294                 clipinfo.startPos = GenTime(position, m_doc->fps());
295                 clipinfo.endPos = clipinfo.startPos + GenTime(out, m_doc->fps());
296                 clipinfo.track = ix;
297                 kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
298                 ClipItem *item = new ClipItem(clip, clipinfo, m_scale, m_doc->fps());
299                 m_scene->addItem(item);
300                 position += out;
301
302                 // parse clip effects
303                 for (QDomNode n2 = elem.firstChild(); !n2.isNull(); n2 = n2.nextSibling()) {
304                     QDomElement effect = n2.toElement();
305                     if (effect.tagName() == "filter") {
306                         // add effect to clip
307                         QString effecttag;
308                         QString effectindex;
309                         // Get effect tag & index
310                         for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
311                             // parse effect parameters
312                             QDomElement effectparam = n3.toElement();
313                             if (effectparam.attribute("name") == "tag") {
314                                 effecttag = effectparam.text();
315                             }
316                             if (effectparam.attribute("name") == "kdenlive_ix") {
317                                 effectindex = effectparam.text();
318                             }
319                         }
320
321                         // get effect standard tags
322                         QDomElement clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag);
323                         clipeffect.setAttribute("kdenlive_ix", effectindex);
324                         QDomNodeList clipeffectparams = clipeffect.childNodes();
325
326                         // adjust effect parameters
327                         for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
328                             // parse effect parameters
329                             QDomElement effectparam = n3.toElement();
330                             QString paramname = effectparam.attribute("name");
331                             QString paramvalue = effectparam.text();
332
333                             // try to find this parameter in the effect xml
334                             QDomElement e;
335                             for (int k = 0; k < clipeffectparams.count(); k++) {
336                                 e = clipeffectparams.item(k).toElement();
337                                 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
338                                     e.setAttribute("value", paramvalue);
339                                     break;
340                                 }
341                             }
342                         }
343                         item->addEffect(clipeffect, false);
344                     }
345                 }
346
347             } else kWarning() << "CANNOT INSERT CLIP " << id;
348             //m_clipList.append(clip);
349         }
350     }
351     //m_trackDuration = position;
352
353
354     //documentTracks.insert(ix, track);
355     kDebug() << "*************  ADD DOC TRACK " << ix << ", DURATION: " << position;
356     return position;
357     //track->show();
358 }
359
360 QGraphicsScene *TrackView::projectScene() {
361     return m_scene;
362 }
363
364 CustomTrackView *TrackView::projectView() {
365     return m_trackview;
366 }
367
368 void TrackView::setEditMode(const QString & editMode) {
369     m_editMode = editMode;
370 }
371
372 const QString & TrackView::editMode() const {
373     return m_editMode;
374 }
375
376 #include "trackview.moc"