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 "kdenlivesettings.h"
32 #include "clipmanager.h"
33 #include "customruler.h"
34 #include "kdenlivedoc.h"
35 #include "mainwindow.h"
36 #include "customtrackview.h"
38 TrackView::TrackView(KdenliveDoc *doc, QWidget *parent)
39 : QWidget(parent), m_doc(doc), m_scale(1.0), m_projectTracks(0), m_currentZoom(4) {
41 view = new Ui::TimeLine_UI();
44 m_scene = new QGraphicsScene();
45 m_trackview = new CustomTrackView(doc, m_scene, parent);
46 m_trackview->scale(1, 1);
47 m_trackview->setAlignment(Qt::AlignLeft | Qt::AlignTop);
48 //m_scene->addRect(QRectF(0, 0, 100, 100), QPen(), QBrush(Qt::red));
50 m_ruler = new CustomRuler(doc->timecode(), m_trackview);
51 QHBoxLayout *layout = new QHBoxLayout;
52 view->ruler_frame->setLayout(layout);
55 layout->getContentsMargins(&left_margin, 0, &right_margin, 0);
56 layout->setContentsMargins(left_margin, 0, right_margin, 0);
57 layout->addWidget(m_ruler);
59 QHBoxLayout *tracksLayout = new QHBoxLayout;
60 tracksLayout->setContentsMargins(0, 0, 0, 0);
61 view->tracks_frame->setLayout(tracksLayout);
63 view->headers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64 view->headers_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 m_headersLayout = new QVBoxLayout;
67 m_headersLayout->setContentsMargins(0, 0, 0, 0);
68 m_headersLayout->setSpacing(0);
69 view->headers_container->setLayout(m_headersLayout);
71 connect(view->headers_area->verticalScrollBar(), SIGNAL(valueChanged(int)), m_trackview->verticalScrollBar(), SLOT(setValue(int)));
73 tracksLayout->addWidget(m_trackview);
75 connect(m_trackview->verticalScrollBar(), SIGNAL(valueChanged(int)), view->headers_area->verticalScrollBar(), SLOT(setValue(int)));
76 connect(m_trackview, SIGNAL(trackHeightChanged()), this, SLOT(slotRebuildTrackHeaders()));
78 parseDocument(doc->toXml());
80 connect(m_trackview, SIGNAL(cursorMoved(int, int)), m_ruler, SLOT(slotCursorMoved(int, int)));
81 connect(m_trackview->horizontalScrollBar(), SIGNAL(valueChanged(int)), m_ruler, SLOT(slotMoveRuler(int)));
82 connect(m_trackview, SIGNAL(mousePosition(int)), this, SIGNAL(mousePosition(int)));
83 connect(m_trackview, SIGNAL(clipItemSelected(ClipItem*)), this, SLOT(slotClipItemSelected(ClipItem*)));
84 connect(m_trackview, SIGNAL(transitionItemSelected(Transition*)), this, SLOT(slotTransitionItemSelected(Transition*)));
85 slotChangeZoom(m_currentZoom);
88 int TrackView::currentZoom() const {
92 int TrackView::duration() const {
93 return m_trackview->duration();
96 int TrackView::tracksNumber() const {
97 return m_projectTracks;
100 int TrackView::inPoint() const {
101 return m_ruler->inPoint();
104 int TrackView::outPoint() const {
105 return m_ruler->outPoint();
108 void TrackView::slotClipItemSelected(ClipItem*c) {
109 emit clipItemSelected(c);
112 void TrackView::slotTransitionItemSelected(Transition *t) {
113 emit transitionItemSelected(t);
116 void TrackView::setDuration(int dur) {
117 m_trackview->setDuration(dur);
118 m_ruler->setDuration(dur);
121 void TrackView::parseDocument(QDomDocument doc) {
123 //kDebug() << "//// DOCUMENT: " << doc.toString();
124 QDomNode props = doc.elementsByTagName("properties").item(0);
125 if (!props.isNull()) {
126 cursorPos = props.toElement().attribute("timeline_position").toInt();
128 QDomNodeList tracks = doc.elementsByTagName("track");
129 QDomNodeList playlists = doc.elementsByTagName("playlist");
131 m_projectTracks = tracks.count();
132 int trackduration = 0;
136 kDebug() << "//////////// TIMELINE FOUND: " << m_projectTracks << " tracks";
137 int pos = m_projectTracks - 1;
138 for (int i = 0; i < m_projectTracks; i++) {
139 e = tracks.item(i).toElement();
140 QString playlist_name = e.attribute("producer");
141 if (playlist_name != "black_track" && playlist_name != "playlistmain") {
142 // find playlist related to this track
144 for (int j = 0; j < m_projectTracks; j++) {
145 p = playlists.item(j).toElement();
146 if (p.attribute("id") == playlist_name) break;
148 videotrack = (e.attribute("hide") != "video");
149 trackduration = slotAddProjectTrack(pos, p, videotrack);
151 kDebug() << " PRO DUR: " << trackduration << ", TRACK DUR: " << duration;
152 if (trackduration > duration) duration = trackduration;
155 m_trackview->setDuration(duration);
156 slotRebuildTrackHeaders();
157 //m_trackview->setCursorPos(cursorPos);
158 //m_scrollBox->setGeometry(0, 0, 300 * zoomFactor(), m_scrollArea->height());
161 void TrackView::slotDeleteClip(int clipId) {
162 m_trackview->deleteClip(clipId);
165 void TrackView::setCursorPos(int pos) {
166 m_trackview->setCursorPos(pos);
169 void TrackView::moveCursorPos(int pos) {
170 m_trackview->setCursorPos(pos, false);
173 void TrackView::slotChangeZoom(int factor) {
175 m_ruler->setPixelPerMark(factor);
176 m_scale = (double) FRAME_SIZE / m_ruler->comboScale[factor]; // m_ruler->comboScale[m_currentZoom] /
177 m_currentZoom = factor;
178 m_trackview->setScale(m_scale);
181 int TrackView::fitZoom() const {
182 int zoom = (int)((duration() + 20 / m_scale) * FRAME_SIZE / m_trackview->width());
184 for (i = 0; i < 13; i++)
185 if (m_ruler->comboScale[i] > zoom) break;
189 const double TrackView::zoomFactor() const {
193 const int TrackView::mapLocalToValue(int x) const {
194 return (int)(x * zoomFactor());
197 KdenliveDoc *TrackView::document() {
201 void TrackView::refresh() {
202 m_trackview->viewport()->update();
205 void TrackView::slotRebuildTrackHeaders() {
206 QList <TrackInfo> list = m_trackview->tracksList();
207 QList<HeaderTrack *> widgets = this->findChildren<HeaderTrack *>();
208 for (int i = 0; i < widgets.count(); i++)
209 delete widgets.at(i);
210 int max = list.count();
211 for (int i = 0; i < max; i++) {
212 HeaderTrack *header = new HeaderTrack(i, list.at(max - i - 1), this);
213 connect(header, SIGNAL(switchTrackVideo(int)), m_trackview, SLOT(slotSwitchTrackVideo(int)));
214 connect(header, SIGNAL(switchTrackAudio(int)), m_trackview, SLOT(slotSwitchTrackAudio(int)));
215 m_headersLayout->addWidget(header);
217 view->headers_container->adjustSize();
220 int TrackView::slotAddProjectTrack(int ix, QDomElement xml, bool videotrack) {
224 info.type = VIDEOTRACK;
226 info.isBlind = false;
228 info.type = AUDIOTRACK;
230 info.isBlind = false;
233 m_trackview->addTrack(info);
235 int trackTop = KdenliveSettings::trackheight() * ix;
238 for (QDomNode n = xml.firstChild(); !n.isNull(); n = n.nextSibling()) {
239 QDomElement elem = n.toElement();
240 if (elem.tagName() == "blank") {
241 position += elem.attribute("length").toInt();
242 } else if (elem.tagName() == "entry") {
244 int in = elem.attribute("in").toInt();
245 int id = elem.attribute("producer").toInt();
246 DocClipBase *clip = m_doc->clipManager()->getClipById(id);
248 int out = elem.attribute("out").toInt();
251 clipinfo.startPos = GenTime(position, m_doc->fps());
252 clipinfo.endPos = GenTime(out, m_doc->fps());
254 kDebug() << "// INSERTING CLIP: " << in << "x" << out << ", track: " << ix << ", ID: " << id << ", SCALE: " << m_scale << ", FPS: " << m_doc->fps();
255 ClipItem *item = new ClipItem(clip, clipinfo, m_scale, m_doc->fps());
256 m_scene->addItem(item);
259 // parse clip effects
260 for (QDomNode n2 = elem.firstChild(); !n2.isNull(); n2 = n2.nextSibling()) {
261 QDomElement effect = n2.toElement();
262 if (effect.tagName() == "filter") {
263 // add effect to clip
266 // Get effect tag & index
267 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
268 // parse effect parameters
269 QDomElement effectparam = n3.toElement();
270 if (effectparam.attribute("name") == "tag") {
271 effecttag = effectparam.text();
273 if (effectparam.attribute("name") == "kdenlive_ix") {
274 effectindex = effectparam.text();
278 // get effect standard tags
279 QDomElement clipeffect = MainWindow::videoEffects.getEffectByTag(effecttag);
280 clipeffect.setAttribute("kdenlive_ix",effectindex);
281 QDomNodeList clipeffectparams = clipeffect.childNodes();
283 // adjust effect parameters
284 for (QDomNode n3 = effect.firstChild(); !n3.isNull(); n3 = n3.nextSibling()) {
285 // parse effect parameters
286 QDomElement effectparam = n3.toElement();
287 QString paramname = effectparam.attribute("name");
288 QString paramvalue = effectparam.text();
290 // try to find this parameter in the effect xml
292 for (int k = 0; k < clipeffectparams.count(); k++) {
293 e = clipeffectparams.item(k).toElement();
294 if (!e.isNull() && e.tagName() == "parameter" && e.attribute("name") == paramname) {
295 e.setAttribute("value", paramvalue);
300 item->addEffect(clipeffect, false);
304 } else kWarning() << "CANNOT INSERT CLIP " << id;
305 //m_clipList.append(clip);
308 //m_trackDuration = position;
311 //documentTracks.insert(ix, track);
312 kDebug() << "************* ADD DOC TRACK " << ix << ", DURATION: " << position;
317 QGraphicsScene *TrackView::projectScene() {
321 CustomTrackView *TrackView::projectView() {
325 void TrackView::setEditMode(const QString & editMode) {
326 m_editMode = editMode;
329 const QString & TrackView::editMode() const {
333 #include "trackview.moc"