1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
25 #include "definitions.h"
26 #include "headertrack.h"
27 #include "trackview.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"
37 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
38 : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_currentZoom(4) {
40 view = new Ui::TimeLine_UI();
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));
49 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
50 QHBoxLayout *layout = new QHBoxLayout;
51 view->ruler_frame->setLayout(layout);
54 layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
55 layout->setContentsMargins(left_margin, 0, right_margin, 0);
56 layout->addWidget(m_ruler);
58 QHBoxLayout *tracksLayout = new QHBoxLayout;
59 tracksLayout->setContentsMargins(0, 0, 0, 0);
60 view->tracks_frame->setLayout(tracksLayout);
62 view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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);
70 connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
72 tracksLayout->addWidget(m_trackview);
74 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
75 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
77 parseDocument(doc->toXml());
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_currentZoom);
87 int TrackView::currentZoom() const {
91 int TrackView::duration() const {
92 return m_trackview->duration();
95 int TrackView::tracksNumber() const {
96 return m_projectTracks;
99 int TrackView::inPoint() const {
100 return m_ruler->inPoint();
103 int TrackView::outPoint() const {
104 return m_ruler->outPoint();
107 void TrackView::slotClipItemSelected(ClipItem*c) {
108 emit clipItemSelected(c);
111 void TrackView::slotTransitionItemSelected(Transition *t) {
112 emit transitionItemSelected(t);
115 void TrackView::setDuration(int dur) {
116 m_trackview->setDuration(dur);
117 m_ruler->setDuration(dur);
120 void TrackView::parseDocument(QDomDocument doc) {
122 // kDebug() << "//// DOCUMENT: " << doc.toString();
123 QDomNode props = doc.elementsByTagName("properties").item(0);
124 if (!props.isNull()) {
125 cursorPos = props.toElement().attribute("timeline_position").toInt();
128 // parse project tracks
129 QDomNodeList tracks = doc.elementsByTagName("track");
130 QDomNodeList playlists = doc.elementsByTagName("playlist");
132 m_projectTracks = tracks.count();
133 int trackduration = 0;
138 int pos = m_projectTracks - 1;
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
146 for (int j = 0; j < m_projectTracks; j++) {
147 p = playlists.item(j).toElement();
148 if (p.attribute("id") == playlist_name) break;
150 videotrack = (e.attribute("hide") != "video");
151 trackduration = slotAddProjectTrack(pos, p, videotrack);
153 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
154 if (trackduration > duration) duration = trackduration;
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;
161 int black_clips = p.childNodes().count();
162 for (int i = 0; i < black_clips; i++)
163 m_doc->loadingProgressed();
164 qApp->processEvents();
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;
180 for (int k = 0; k < transitionparams.count(); k++) {
181 p = transitionparams.item(k).toElement();
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)";
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();
195 // Transition should be added to the scene
196 ItemInfo transitionInfo;
197 QDomElement base = MainWindow::transitions.getEffectByTag(mlt_service, QString());
199 for (int k = 0; k < transitionparams.count(); k++) {
200 p = transitionparams.item(k).toElement();
202 QString paramName = p.attribute("name");
203 QString paramValue = p.text();
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");
211 if (!factor.isEmpty()) {
212 double val = paramValue.toDouble() * factor.toDouble();
213 paramValue = QString::number(val);
216 e.setAttribute("value", paramValue);
224 doc.appendChild(doc.importNode(base, true));
225 kDebug() << "/////// TRANSITION XML: "<< doc.toString();*/
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_scale, m_doc->fps(), base);
232 m_scene->addItem(tr);
237 QDomNodeList guides = doc.elementsByTagName("guide");
238 kDebug() << "//////////// OPENING PRJECT FILE, FOUND: " << guides.count() << " GUIDES$$$$$$$$$";
239 for (int i = 0; i < guides.count(); i++) {
240 e = guides.item(i).toElement();
241 const QString comment = e.attribute("comment");
242 const GenTime pos = GenTime(e.attribute("time").toDouble());
243 m_trackview->addGuide(pos, comment);
246 m_trackview->setDuration(duration);
247 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
248 slotRebuildTrackHeaders();
249 //m_trackview->setCursorPos(cursorPos);
250 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
253 void TrackView::slotDeleteClip(int clipId) {
254 m_trackview->deleteClip(clipId);
257 void TrackView::setCursorPos(int pos) {
258 m_trackview->setCursorPos(pos);
261 void TrackView::moveCursorPos(int pos) {
262 m_trackview->setCursorPos(pos, false);
265 void TrackView::slotChangeZoom(int factor) {
267 m_ruler->setPixelPerMark(factor);
268 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
269 m_currentZoom = factor;
270 m_trackview->setScale(m_scale);
273 int TrackView::fitZoom() const {
274 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
276 for (i = 0; i < 13; i++)
277 if (m_ruler->comboScale[i] > zoom) break;
281 const double TrackView::zoomFactor() const {
285 const int TrackView::mapLocalToValue(int x) const {
286 return (int)(x * zoomFactor());
289 KdenliveDoc *TrackView::document() {
293 void TrackView::refresh() {
294 m_trackview->viewport()->update();
297 void TrackView::slotRebuildTrackHeaders() {
298 QList <TrackInfo> list = m_trackview->tracksList();
299 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
300 for (int i = 0; i < widgets.count(); i++)
301 delete widgets.at(i);
302 int max = list.count();
303 for (int i = 0; i < max; i++) {
304 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
305 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
306 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
307 m_headersLayout->addWidget(header);
309 view->headers_container->adjustSize();
312 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
316 info.type = VIDEOTRACK;
318 info.isBlind = false;
320 info.type = AUDIOTRACK;
322 info.isBlind = false;
325 m_trackview->addTrack(info);
327 int trackTop = KdenliveSettings::trackheight() * ix;
330 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
331 QDomElement elem = n.toElement();
332 if (elem.tagName() == "blank") {
333 position += elem.attribute("length").toInt();
334 } else if (elem.tagName() == "entry") {
335 m_doc->loadingProgressed();
336 qApp->processEvents();
338 int in = elem.attribute("in").toInt();
339 int id = elem.attribute("producer").toInt();
340 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
342 int out = elem.attribute("out").toInt();
345 clipinfo.startPos = GenTime(position, m_doc->fps());
346 clipinfo.endPos = clipinfo.startPos + GenTime(out - in, m_doc->fps());
348 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
349 ClipItem *item = new ClipItem(clip, clipinfo, GenTime(in, m_doc->fps()), m_scale, m_doc->fps());
350 m_scene->addItem(item);
351 position += (out - in);
353 // parse clip effects
354 for (QDomNode n2 = elem.firstChild(); !n2.isNull(); n2 = n2.nextSibling()) {
355 QDomElement effect = n2.toElement();
356 if (effect.tagName() == "filter") {
357 // add effect to clip
361 // Get effect tag & index
362 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
363 // parse effect parameters
364 QDomElement effectparam = n3.toElement();
365 if (effectparam.attribute("name") == "tag") {
366 effecttag = effectparam.text();
368 if (effectparam.attribute("name") == "kdenlive_id") {
369 effectid = effectparam.text();
371 if (effectparam.attribute("name") == "kdenlive_ix") {
372 effectindex = effectparam.text();
376 // get effect standard tags
377 QDomElement clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag, effectid);
378 if (clipeffect.isNull()) clipeffect = MainWindow::audioEffects.getEffectByTag(effecttag, effectid);
379 if (clipeffect.isNull()) clipeffect = MainWindow::customEffects.getEffectByTag(effecttag, effectid);
380 clipeffect.setAttribute("kdenlive_ix", effectindex);
381 QDomNodeList clipeffectparams = clipeffect.childNodes();
383 // adjust effect parameters
384 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
385 // parse effect parameters
386 QDomElement effectparam = n3.toElement();
387 QString paramname = effectparam.attribute("name");
388 QString paramvalue = effectparam.text();
390 // try to find this parameter in the effect xml
392 for (int k = 0; k < clipeffectparams.count(); k++) {
393 e = clipeffectparams.item(k).toElement();
394 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
395 e.setAttribute("value", paramvalue);
400 item->addEffect(clipeffect, false);
404 } else kWarning() << "CANNOT INSERT CLIP " << id;
405 //m_clipList.append(clip);
408 //m_trackDuration = position;
411 //documentTracks.insert(ix, track);
412 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
417 QGraphicsScene *TrackView::projectScene() {
421 CustomTrackView *TrackView::projectView() {
425 void TrackView::setEditMode(const QString & editMode) {
426 m_editMode = editMode;
429 const QString & TrackView::editMode() const {
433 #include "trackview.moc"