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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
27 #include "definitions.h"
28 #include "headertrack.h"
29 #include "trackview.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"
39 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
40 : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_currentZoom(4) {
42 view = new Ui::TimeLine_UI();
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));
51 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
52 QHBoxLayout *layout = new QHBoxLayout;
53 view->ruler_frame->setLayout(layout);
56 layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
57 layout->setContentsMargins(left_margin, 0, right_margin, 0);
58 layout->addWidget(m_ruler);
60 QHBoxLayout *tracksLayout = new QHBoxLayout;
61 tracksLayout->setContentsMargins(0, 0, 0, 0);
62 view->tracks_frame->setLayout(tracksLayout);
64 view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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);
72 connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
74 tracksLayout->addWidget(m_trackview);
76 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
77 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
79 parseDocument(doc->toXml());
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);
89 int TrackView::currentZoom() const {
93 int TrackView::duration() const {
94 return m_trackview->duration();
97 int TrackView::tracksNumber() const {
98 return m_projectTracks;
101 int TrackView::inPoint() const {
102 return m_ruler->inPoint();
105 int TrackView::outPoint() const {
106 return m_ruler->outPoint();
109 void TrackView::slotClipItemSelected(ClipItem*c) {
110 emit clipItemSelected(c);
113 void TrackView::slotTransitionItemSelected(Transition *t) {
114 emit transitionItemSelected(t);
117 void TrackView::setDuration(int dur) {
118 m_trackview->setDuration(dur);
119 m_ruler->setDuration(dur);
122 void TrackView::parseDocument(QDomDocument doc) {
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();
130 // parse project tracks
131 QDomNodeList tracks = doc.elementsByTagName("track");
132 QDomNodeList playlists = doc.elementsByTagName("playlist");
134 m_projectTracks = tracks.count();
135 int trackduration = 0;
140 int pos = m_projectTracks - 1;
142 for (int i = 0; i < m_projectTracks; i++) {
143 e = tracks.item(i).toElement();
144 QString playlist_name = e.attribute("producer");
145 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
146 // find playlist related to this track
148 for (int j = 0; j < m_projectTracks; j++) {
149 p = playlists.item(j).toElement();
150 if (p.attribute("id") == playlist_name) break;
152 videotrack = (e.attribute("hide") != "video");
153 trackduration = slotAddProjectTrack(pos, p, videotrack);
155 //kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
156 if (trackduration > duration) duration = trackduration;
158 // background black track
159 for (int j = 0; j < m_projectTracks; j++) {
160 p = playlists.item(j).toElement();
161 if (p.attribute("id") == playlist_name) break;
163 int black_clips = p.childNodes().count();
164 for (int i = 0; i < black_clips; i++)
165 m_doc->loadingProgressed();
166 qApp->processEvents();
172 QDomNodeList transitions = doc.elementsByTagName("transition");
173 int projectTransitions = transitions.count();
174 //kDebug() << "//////////// TIMELINE FOUND: " << projectTransitions << " transitions";
175 for (int i = 0; i < projectTransitions; i++) {
176 e = transitions.item(i).toElement();
177 QDomNodeList transitionparams = e.childNodes();
178 bool transitionAdd = true;
182 for (int k = 0; k < transitionparams.count(); k++) {
183 p = transitionparams.item(k).toElement();
185 // do not add audio mixing transitions
186 if (p.attribute("name") == "internal_added" && p.text() == "237") {
187 transitionAdd = false;
188 //kDebug() << "// TRANSITRION " << i << " IS NOT VALID (INTERN ADDED)";
190 } else if (p.attribute("name") == "a_track") a_track = p.text().toInt();
191 else if (p.attribute("name") == "b_track") b_track = m_projectTracks - 1 - p.text().toInt();
192 else if (p.attribute("name") == "mlt_service") mlt_service = p.text();
196 // Transition should be added to the scene
197 ItemInfo transitionInfo;
198 transitionInfo.startPos = GenTime(e.attribute("in").toInt(), m_doc->fps());
199 transitionInfo.endPos = GenTime(e.attribute("out").toInt(), m_doc->fps());
200 transitionInfo.track = b_track;
201 //kDebug() << "/////////////// +++++++++++ ADDING TRANSITION ON TRACK: " << b_track << ", TOTAL TRKA: " << m_projectTracks;
202 Transition *tr = new Transition(transitionInfo, a_track, m_scale, m_doc->fps(), QDomElement());
203 m_scene->addItem(tr);
208 m_trackview->setDuration(duration);
209 kDebug() << "/////////// TOTAL PROJECT DURATION: " << duration;
210 slotRebuildTrackHeaders();
211 //m_trackview->setCursorPos(cursorPos);
212 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
215 void TrackView::slotDeleteClip(int clipId) {
216 m_trackview->deleteClip(clipId);
219 void TrackView::setCursorPos(int pos) {
220 m_trackview->setCursorPos(pos);
223 void TrackView::moveCursorPos(int pos) {
224 m_trackview->setCursorPos(pos, false);
227 void TrackView::slotChangeZoom(int factor) {
229 m_ruler->setPixelPerMark(factor);
230 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
231 m_currentZoom = factor;
232 m_trackview->setScale(m_scale);
235 int TrackView::fitZoom() const {
236 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
238 for (i = 0; i < 13; i++)
239 if (m_ruler->comboScale[i] > zoom) break;
243 const double TrackView::zoomFactor() const {
247 const int TrackView::mapLocalToValue(int x) const {
248 return (int)(x * zoomFactor());
251 KdenliveDoc *TrackView::document() {
255 void TrackView::refresh() {
256 m_trackview->viewport()->update();
259 void TrackView::slotRebuildTrackHeaders() {
260 QList <TrackInfo> list = m_trackview->tracksList();
261 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
262 for (int i = 0; i < widgets.count(); i++)
263 delete widgets.at(i);
264 int max = list.count();
265 for (int i = 0; i < max; i++) {
266 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
267 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
268 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
269 m_headersLayout->addWidget(header);
271 view->headers_container->adjustSize();
274 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
278 info.type = VIDEOTRACK;
280 info.isBlind = false;
282 info.type = AUDIOTRACK;
284 info.isBlind = false;
287 m_trackview->addTrack(info);
289 int trackTop = KdenliveSettings::trackheight() * ix;
292 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
293 QDomElement elem = n.toElement();
294 if (elem.tagName() == "blank") {
295 position += elem.attribute("length").toInt();
296 } else if (elem.tagName() == "entry") {
297 m_doc->loadingProgressed();
298 qApp->processEvents();
300 int in = elem.attribute("in").toInt();
301 int id = elem.attribute("producer").toInt();
302 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
304 int out = elem.attribute("out").toInt();
307 clipinfo.startPos = GenTime(position, m_doc->fps());
308 clipinfo.endPos = clipinfo.startPos + GenTime(out - in, m_doc->fps());
310 //kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
311 ClipItem *item = new ClipItem(clip, clipinfo, GenTime(in, m_doc->fps()), m_scale, m_doc->fps());
312 m_scene->addItem(item);
313 position += (out - in);
315 // parse clip effects
316 for (QDomNode n2 = elem.firstChild(); !n2.isNull(); n2 = n2.nextSibling()) {
317 QDomElement effect = n2.toElement();
318 if (effect.tagName() == "filter") {
319 // add effect to clip
322 // Get effect tag & index
323 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
324 // parse effect parameters
325 QDomElement effectparam = n3.toElement();
326 if (effectparam.attribute("name") == "tag") {
327 effecttag = effectparam.text();
329 if (effectparam.attribute("name") == "kdenlive_ix") {
330 effectindex = effectparam.text();
334 // get effect standard tags
335 QDomElement clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag);
336 clipeffect.setAttribute("kdenlive_ix", effectindex);
337 QDomNodeList clipeffectparams = clipeffect.childNodes();
339 // adjust effect parameters
340 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
341 // parse effect parameters
342 QDomElement effectparam = n3.toElement();
343 QString paramname = effectparam.attribute("name");
344 QString paramvalue = effectparam.text();
346 // try to find this parameter in the effect xml
348 for (int k = 0; k < clipeffectparams.count(); k++) {
349 e = clipeffectparams.item(k).toElement();
350 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
351 e.setAttribute("value", paramvalue);
356 item->addEffect(clipeffect, false);
360 } else kWarning() << "CANNOT INSERT CLIP " << id;
361 //m_clipList.append(clip);
364 //m_trackDuration = position;
367 //documentTracks.insert(ix, track);
368 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
373 QGraphicsScene *TrackView::projectScene() {
377 CustomTrackView *TrackView::projectView() {
381 void TrackView::setEditMode(const QString & editMode) {
382 m_editMode = editMode;
385 const QString & TrackView::editMode() const {
389 #include "trackview.moc"